Tree Traversals (25)
来源:互联网 发布:require.js 2.3.5 编辑:程序博客网 时间:2024/06/08 09:31
题目描述
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
输入描述:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
输出描述:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
输入例子:
72 3 1 5 7 6 41 2 3 4 5 6 7
输出例子:
4 1 6 3 5 7 2
我的代码:
#include<iostream>#include<queue>using namespace std;struct Node{ int data; Node *lchild,*rchild;};Node *creat(int h[],int z[],int n){ Node *t;int i; if(n==0) return NULL; t=new Node; t->data=h[n-1]; for(i=0;i<n;i++) { if(z[i]==h[n-1]) break; } t->lchild=creat(h,z,i); t->rchild=creat(h+i,z+i+1,n-i-1); return t;}void ceng(Node *t){ queue<Node*>q; int a[101],i,k=0; if(t) { q.push(t); while(!q.empty()) { t=q.front(); q.pop(); a[k++]=t->data; if(t->lchild) q.push(t->lchild); if(t->rchild) q.push(t->rchild); } } for(i=0;i<k;i++){if(i==0) cout<<a[i];else cout<<" "<<a[i];}}int main(){ int n,i,b[101],c[101]; cin>>n; for(i=0;i<n;i++) cin>>b[i]; for(i=0;i<n;i++) cin>>c[i]; Node *r=creat(b,c,n); ceng(r); return 0;}
阅读全文
0 0
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020 Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- pat1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- Tree Traversals Again (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 特性和反射
- 2-Add Two Numbers
- xml配置文件---ssm完美整合(学习笔记)
- 第二章 SQL命令参考-CREATE TABLE AS
- 洛谷P2330 [SCOI2005]繁忙的都市
- Tree Traversals (25)
- 陈默:如何做不焦虑的家长 喜马拉雅课程付费百度云网盘资源下载mp3免费音频
- strlen计算字符串长度
- 动态规划(1)——斐波那契数列
- PDO扩展
- 阿里巴巴Java开发手册Eclipse插件 使用指南
- forward与redirect的认识
- 编码问题
- 【20171019】python_语言设计(10)库与软件