当前位置:网站首页>建立与遍历二叉树
建立与遍历二叉树
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
边栏推荐
- Niuke white moon race 6 [solution]
- [Euler plan question 13] sum of large numbers
- Use of MySQL command line client and common commands
- JS relearning
- Source Generator实战
- tf. keras. layers. Inputlayer function
- Opencv reads webcam video and saves it locally
- Tips in MATLAB
- Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
- Chapter V project quality management of information system project manager summary
猜你喜欢
最通俗易懂的依赖注入之生命周期
BLDC double closed loop (speed PI + current PI) Simulink simulation model
Detailed explanation of distributed things
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
How to use C language to realize [guessing numbers game]
Liunx foundation - zabbix5 0 monitoring system installation and deployment
Onenet connection process
最通俗易懂的依赖注入与控制反转
Realize QQ login with PHP
How to write the expected salary on your resume to double your salary during the interview?
随机推荐
C# 11 对 ref 和 struct 的改进
Source Generator实战
Numpy stack function
[Euler plan question 13] sum of large numbers
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
C#中切片语法糖的使用
Creating wechat voucher process with PHP
tf. keras. layers. Density function
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
【鉴权/授权】自定义一个身份认证Handler
In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (10)
Introduction to ACM [inclusion exclusion theorem]
How to deploy a website with only a server and no domain name?
JS learning notes
Basic workflow of CPU
Guangcheng cloud service can fill in a daily report regularly every day
Q-Learning & Sarsa
Configuring Apache Web services for servers such as Tianyi cloud