现有一棵n个结点的二叉树(结点编号为从0到n-1,根结点为0号结点),求这棵二叉树的中序遍历序列。
第一行一个整数n(1≤n≤50),表示二叉树的结点个数;
接下来n行,每行一个结点,按顺序给出编号从0到n-1的结点的左子结点编号和右子结点编号,中间用空格隔开。如果不存在对应的子结点,那么用-1表示。
输出n个整数,表示中序遍历序列,中间用空格隔开,行末不允许有多余的空格。
6 2 5 -1 -1 1 4 -1 -1 -1 -1 -1 3
1 2 4 0 5 3
- #include
- using namespace std;
- struct Node{
- int l,r;
- }node[1005];
- vector<int> in;
-
- void inorder(int root){
- if(root==-1){
- return;
- }
- inorder(node[root].l);
- in.push_back(root);
- inorder(node[root].r);
- }
- int main(){
- int n;
- cin>>n;
- for(int i = 0;i
- cin>>node[i].l>>node[i].r;
- }
- inorder(0);
- for(int i = 0;i
size();i++){ - cout<
- if(i!=in.size()-1){
- cout<<" ";
- }
- }
- }
-
相关阅读:
如何利用分层测试概念设计针对性测试用例
android读取sd卡上文件中的数据
2022谷粒商城学习笔记(八)规格参数相关功能
【HMS】地图服务我的位置定位问题
Ubuntu24.04基本配置
【ENOVIA 服务包】知识重用解决方案 | 达索系统百世慧®
(一)MySQL_数据库概述技术总结
更改linux centos 7系统语言
CENTURY模型可以模拟土壤呼吸吗?CENTURY模型实践技术应用与案例分析
2022年软件测试人员必读的经典书籍推荐(附电子版)
-
原文地址:https://blog.csdn.net/m0_63942435/article/details/136601299