现有一棵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<<" ";
- }
- }
- }
-
相关阅读:
React之引入css的方式
大数据ClickHouse进阶(二十四):ClickHouse权限管理
新白娘子传奇系列
Spring的“一站式解决方案”体现在哪里?
PostMan 测试
10月工作经验总结
shell之循环语句(for、while、until)
面经综合总结
Azure DevOps (十) 通过流水线完成Docker镜像的部署
05 proxy_pass 携带有 uri 的场景下面的处理
-
原文地址:https://blog.csdn.net/m0_63942435/article/details/136601299