当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
- 【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
- 股票开户哪里好?网上客户经理开户安全吗
- 如何安全快速地从 Centos迁移到openEuler
- B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
- Android interview classic, 2022 Android interview written examination summary
- Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
- leetcode刷题:二叉树12(二叉树的所有路径)
- [quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design
- C - sequential structure
猜你喜欢

No matter how busy you are, you can't forget safety

Fundamentals of deep learning convolutional neural network (CNN)

leetcode刷题:二叉树16(路径总和)

618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?

Jvmrandom cannot set seeds | problem tracing | source code tracing

Leetcode brush questions: binary tree 18 (largest binary tree)

How to select the Block Editor? Impression notes verse, notation, flowus

Go language | 02 for loop and the use of common functions

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

Database logic processing function
随机推荐
nprogress插件 进度条
Cocos2d-x项目总结中的一些遇到的问题
A solution to PHP's inability to convert strings into JSON
Is it safe for Anxin securities to open an account online?
Analysis of openh264 decoded data flow
Common operators and operator priority
leetcode刷题:二叉树15(找树左下角的值)
【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
走入并行的世界
Summer Challenge harmonyos - realize message notification function
Fundamentals of deep learning convolutional neural network (CNN)
Redis cluster simulated message queue
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
通配符选择器
leetcode刷题:二叉树16(路径总和)
Tasks in GStreamer
ffplay文档[通俗易懂]
Using repositoryprovider to simplify the value passing of parent-child components
14. Users, groups, and permissions (14)