当前位置:网站首页>Informatics Olympiad 1340: [example 3-5] extended binary tree
Informatics Olympiad 1340: [example 3-5] extended binary tree
2022-07-05 20:18:00 【Jun Yi_ noip】
【 Topic link 】
ybt 1340:【 example 3-5】 Expand the binary tree
【 Topic test site 】
1. Binary tree
【 Their thinking 】
solution 1:
use getchar() or cin.get() Read one character at a time .
The format of the preorder sequence is :< Root node > < The first sequence of the left subtree > < The precedence sequence of the right subtree >
A binary tree can be constructed recursively :
- Read in a character , As the root node
- Recursive construction of left subtree
- Recursively build the right subtree
- Connect the left and right subtrees under the root node of this tree , Return the address of the root node
After building the tree , Then output the middle order and post order traversal sequence of this tree .
【 Solution code 】
solution 1:
#include <bits/stdc++.h>
using namespace std;
struct Node// Binary tree node
{
char val;
int left, right;
};
Node node[1000];// Node pool
int p = 1;
void inOrder(int r)// In the sequence traversal
{
if(r != 0)
{
inOrder(node[r].left);
cout << node[r].val;
inOrder(node[r].right);
}
}
void postOrder(int r)// After the sequence traversal
{
if(r != 0)
{
postOrder(node[r].left);
postOrder(node[r].right);
cout << node[r].val;
}
}
int createTree(char val)// Pass in the value of the root of the binary tree to be generated , Returns the address of the generated binary tree
{
int np;
if(val == '.')
return 0;
else
{
np = p++;
node[np].val = val;
node[np].left = createTree(cin.get());// Read in character , Build the left subtree
node[np].right = createTree(cin.get());// Read in character , Build the right subtree
return np;
}
}
int main()
{
int root = createTree(cin.get());
inOrder(root);
cout << endl;
postOrder(root);
return 0;
}
边栏推荐
- 14. Users, groups, and permissions (14)
- 【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
- 微信小程序正则表达式提取链接
- Go language learning tutorial (16)
- sun. misc. Base64encoder error reporting solution [easy to understand]
- PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- 计算lnx的一种方式
- Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
- js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
- Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
猜你喜欢
Securerandom things | true and false random numbers
计算lnx的一种方式
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
Leetcode skimming: binary tree 12 (all paths of binary tree)
.Net分布式事務及落地解决方案
实操演示:产研团队如何高效构建需求工作流?
Leetcode brush question: binary tree 14 (sum of left leaves)
leetcode刷题:二叉树11(平衡二叉树)
死信队列入门(两个消费者,一个生产者)
关于BRAM IP复位的优先级
随机推荐
Some problems encountered in cocos2d-x project summary
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
Summer Challenge harmonyos - realize message notification function
July 4, 2022 - July 10, 2022 (UE4 video tutorial MySQL)
When JS method passes long type ID value, precision loss will occur
Codeforces Round #804 (Div. 2) - A, B, C
[C language] three implementations of quick sorting and optimization details
Station B up builds the world's first pure red stone neural network, pornographic detection based on deep learning action recognition, Chen Tianqi's course progress of machine science compilation MLC,
Go language learning tutorial (XV)
淺淺的談一下ThreadLocalInsecureRandom
Bzoj 3747 poi2015 kinoman segment tree
Wechat applet regular expression extraction link
[C language] merge sort
本季度干货导航 | 2022年Q2
Cocos2d-x项目总结中的一些遇到的问题
Reinforcement learning - learning notes 4 | actor critical
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
MySql的root密码忘记该怎么找回
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design