当前位置:网站首页>建立与遍历二叉树
建立与遍历二叉树
2022-04-23 03:03:00 【学习kl&tk】
以字符串的形式定义一棵二叉树的先序序列,若字符是‘#’, 表示该二叉树是空树,否则该字符是相应结点的数据元素。读入相应先序序列,建立二叉链式存储结构的二叉树,然后中序遍历该二叉树并输出结点数据。
输入样例:
在这里给出一组输入。例如:
ABC##DE#G##F###
输出样例:
在这里给出相应的输出。例如:
CBEGDFA
#include "bits/stdc++.h"
using namespace std;
struct Tree{
char val;
Tree* lchild;
Tree* rchild;
};
Tree* build(){
char c;
cin >> c;
if(c=='#') return NULL;
Tree* root = new Tree();
root->val = c;
root->lchild = build();
root->rchild = build();
return root;
}
void dfs(Tree* root){
if(root == NULL) return;
dfs(root->lchild);
cout << root->val ;
dfs(root->rchild);
}
int main()
{
Tree* root = build();
dfs(root);
return 0;
}
版权声明
本文为[学习kl&tk]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_53013914/article/details/124278159
边栏推荐
- Encapsulation of ele table
- Sonic cloud real machine tutorial
- How to use C language to realize [guessing numbers game]
- BLDC double closed loop (speed PI + current PI) Simulink simulation model
- Blazor University (12)组件 — 组件生命周期
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (10)
- SQL statement - DDL
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- Passing object type parameters through openfeign
- FileNotFoundError: [Errno 2] No such file or directory
猜你喜欢

Encapsulate components such as pull-down menu based on ele

Blazor University (12)组件 — 组件生命周期

Array and collection types passed by openfeign parameters

Processes and threads

Recursion - outputs continuously increasing numbers

Realize QQ login with PHP
![FileNotFoundError: [Errno 2] No such file or directory](/img/ea/0c3f2768d14c1f4bb42bd1309ab996.png)
FileNotFoundError: [Errno 2] No such file or directory

tf. keras. layers. MaxPooling? D function

AOT和单文件发布对程序性能的影响

Plug in for vscode
随机推荐
一套关于 内存对齐 的C#面试题,做错的人很多!
Opencv combines multiple pictures into video
Reverse a linked list < difficulty coefficient >
Configuring Apache Web services for servers such as Tianyi cloud
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
JSON data text
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
Wepy learning record
Source Generator实战
Chapter VI project information management system summary
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Sonic cloud real machine tutorial
tf. keras. layers. Density function
Openfeign service call
Passing object type parameters through openfeign
Response processing of openfeign
Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
Opencv fills the rectangle with a transparent color