当前位置:网站首页>信息学奥赛一本通 1340:【例3-5】扩展二叉树
信息学奥赛一本通 1340:【例3-5】扩展二叉树
2022-07-05 20:10:00 【君义_noip】
【题目链接】
【题目考点】
1. 二叉树
【解题思路】
解法1:
用getchar()或cin.get()每次读取一个字符。
先序序列的格式为:<根结点> <左子树的先序序列> <右子树的先序序列>
可以用递归的方法构建二叉树:
- 读入一个字符,作为根结点
- 递归构建左子树
- 递归构建右子树
- 将左右子树接在这棵树根结点的下面,返回根结点的地址
完成树的构建后,再输出这棵树的中序和后序遍历序列。
【题解代码】
解法1:
#include <bits/stdc++.h>
using namespace std;
struct Node//二叉树结点
{
char val;
int left, right;
};
Node node[1000];//结点池
int p = 1;
void inOrder(int r)//中序遍历
{
if(r != 0)
{
inOrder(node[r].left);
cout << node[r].val;
inOrder(node[r].right);
}
}
void postOrder(int r)//后序遍历
{
if(r != 0)
{
postOrder(node[r].left);
postOrder(node[r].right);
cout << node[r].val;
}
}
int createTree(char val)//传入要生成的二叉树的根的值,返回生成的二叉树的地址
{
int np;
if(val == '.')
return 0;
else
{
np = p++;
node[np].val = val;
node[np].left = createTree(cin.get());//读入字符,构建左子树
node[np].right = createTree(cin.get());//读入字符,构建右子树
return np;
}
}
int main()
{
int root = createTree(cin.get());
inOrder(root);
cout << endl;
postOrder(root);
return 0;
}
边栏推荐
- [quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
- 走入并行的世界
- DP: tree DP
- [C language] merge sort
- 淺淺的談一下ThreadLocalInsecureRandom
- Database logic processing function
- Go language learning tutorial (16)
- JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
- 2023年深圳市绿色低碳产业扶持计划申报指南
- Is it safe for CICC fortune to open an account online?
猜你喜欢

Wechat applet regular expression extraction link

kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)

CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)

解决php无法将string转换为json的办法

Interviewer: what is the internal implementation of set data types in redis?

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

淺淺的談一下ThreadLocalInsecureRandom
![[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

如何安全快速地从 Centos迁移到openEuler
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
随机推荐
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
Build your own website (16)
图嵌入Graph embedding学习笔记
淺淺的談一下ThreadLocalInsecureRandom
Some problems encountered in cocos2d-x project summary
Is it safe for Galaxy Securities to open an account online?
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
深度學習 卷積神經網絡(CNN)基礎
微信小程序正则表达式提取链接
. Net distributed transaction and landing solution
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
计算lnx的一种方式
Debezium series: modify the source code to support drop foreign key if exists FK
14. Users, groups, and permissions (14)
Android interview classic, 2022 Android interview written examination summary
解决php无法将string转换为json的办法
ICTCLAS用的字Lucene4.9捆绑
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
怎么挑选好的外盘平台,安全正规的?
C - sequential structure