当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
![[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design](/img/92/7af0db21b3d7892bdc5dce50ca332e.png)
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design

物联网智能家居基本方法实现之经典

Zero cloud new UI design

A solution to PHP's inability to convert strings into JSON

死信队列入门(两个消费者,一个生产者)

关于BRAM IP复位的优先级

- Oui. Net Distributed Transaction and Landing Solution

【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)

ROS2专题【01】:win10上安装ROS2

Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)
随机推荐
Mongodb/ document operation
信息学奥赛一本通 1337:【例3-2】单词查找树 | 洛谷 P5755 [NOI2000] 单词查找树
BZOJ 3747 POI2015 Kinoman 段树
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
leetcode刷题:二叉树15(找树左下角的值)
Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
中金财富在网上开户安全吗?
Leetcode(695)——岛屿的最大面积
ICTCLAS word Lucene 4.9 binding
model方法
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
Summer Challenge harmonyos - realize message notification function
物联网智能家居基本方法实现之经典
. Net distributed transaction and landing solution
【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value