当前位置:网站首页>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;
}
边栏推荐
- Leetcode skimming: binary tree 16 (path sum)
- Leetcode brush question: binary tree 13 (the same tree)
- 微信小程序正则表达式提取链接
- mongodb基操的练习
- Enter the parallel world
- 2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
- Introduction to dead letter queue (two consumers, one producer)
- What is PyC file
- [quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
- 中金财富在网上开户安全吗?
猜你喜欢

kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)
![[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

js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)

Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?

淺淺的談一下ThreadLocalInsecureRandom

Jvmrandom cannot set seeds | problem tracing | source code tracing

Parler de threadlocal insecurerandom

微信小程序正则表达式提取链接

Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)

- Oui. Net Distributed Transaction and Landing Solution
随机推荐
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
Enter the parallel world
leetcode刷题:二叉树14(左叶子之和)
leetcode刷题:二叉树12(二叉树的所有路径)
Elk distributed log analysis system deployment (Huawei cloud)
CTF reverse Foundation
Summer Challenge harmonyos - realize message notification function
Flume series: interceptor filtering data
leetcode刷题:二叉树10(完全二叉树的节点个数)
Zero cloud new UI design
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
Wechat applet regular expression extraction link
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
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
【c语言】快速排序的三种实现以及优化细节
Leetcode brush questions: binary tree 18 (largest binary tree)
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
Practical demonstration: how can the production research team efficiently build the requirements workflow?
Debezium series: modify the source code to support drop foreign key if exists FK
【c语言】归并排序