博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1020. Tree Traversals (25)
阅读量:6852 次
发布时间:2019-06-26

本文共 3375 字,大约阅读时间需要 11 分钟。

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.

Input Specification:

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.

Output Specification:

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.

Sample Input:
72 3 1 5 7 6 41 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2

编译器
Assembler (nasm)Basic (blassic)C (gcc)C# (gmcs)C++ (g++)Go (gccgo)Haskell (ghc)Java (java)Javascript (js)Lisp (clisp)Lua (lua)Pascal (fpc)Perl (perl)PHP (php)Plaintext (cat)Python (python3)Python (python2)Ruby (ruby)Scheme (guile)Shell (bash)Vala (valac)VisualBasic (vbnc)
 
代码
#include <map> #include <string> #include<algorithm> #include <iostream> #include<queue> using namespace std; int postorder[35]; int inorder[35]; int m; typedef struct tn { struct tn* letf; struct tn* right; int value; }treenode; queue<treenode *> qu; treenode * createtree(int pl,int pr,int il,int ir) { int p; treenode * tt=new treenode; if(ir<il||pr<pl)return NULL; /* else if(ir==il) { tt->value =inorder[ir]; // printf("%d ",tt->value); tt->letf=tt->right=NULL; return tt; }*/ else { tt->value=postorder[pr]; // printf("%d ",tt->value); for(p=il;p<m;p++) if(inorder[p]==postorder[pr]) break; // printf("*%d*",p); tt->letf=createtree(pl,pl+p-il-1,il,p-1); tt->right=createtree(pl+p-il,pr-1,p+1,ir); } return tt; } void levelorder(treenode* t) { treenode* ttt; int first=1; if(t!=NULL) qu.push(t); while(!qu.empty()) { ttt=qu.front (); qu.pop(); if(first) first=0; else printf(" "); printf("%d",ttt->value); if(ttt->letf!=NULL) qu.push (ttt->letf); if(ttt->right!=NULL) qu.push (ttt->right); } } int main() { // freopen("data.in","r",stdin); int n,i,j,k; scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d",&postorder[i]); } for(i=0;i<m;i++) scanf("%d",&inorder[i]); treenode* root; root=createtree(0,m-1,0,m-1); levelorder(root); return 0; }
#include 
#include
#include
#include
using namespace std;int postorder[35];int inorder[35];int m;typedef struct tn{ struct tn* letf; struct tn* right; int value;}treenode;queue
qu;treenode * createtree(int pl,int pr,int il,int ir){ int p; treenode * tt=new treenode; if(ir
value=postorder[pr];// printf("%d ",tt->value); for(p=il;p
letf=createtree(pl,pl+p-il-1,il,p-1); tt->right=createtree(pl+p-il,pr-1,p+1,ir); } return tt;} void levelorder(treenode* t){ treenode* ttt; int first=1; if(t!=NULL) qu.push(t); while(!qu.empty()) { ttt=qu.front (); qu.pop(); if(first) first=0; else printf(" "); printf("%d",ttt->value); if(ttt->letf!=NULL) qu.push (ttt->letf); if(ttt->right!=NULL) qu.push (ttt->right); }}int main(){ freopen("data.in","r",stdin); int n,i,j,k; scanf("%d",&m); for(i=0;i

  

转载于:https://www.cnblogs.com/scjyldq/archive/2012/10/06/2713006.html

你可能感兴趣的文章
洛谷 3804 【模板】后缀自动机
查看>>
LOJ 2736 「JOISC 2016 Day 3」回转寿司 ——堆+分块思路
查看>>
IE8爆出0day,影响所有版本Windows
查看>>
php: Cannot send session cache limiter
查看>>
子类复制父类的值
查看>>
例题10-1 UVa11582 Colossal Fibonacci Numbers!(同余与模算术)
查看>>
hdu 1385 题意 测试数据
查看>>
Java 炫舞按键功能 DancingPlay (整理)
查看>>
rtems网络移植-rtems系统初始化过程分析
查看>>
re正则表达式:import re ;re.search()
查看>>
介绍下Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果。
查看>>
math.net 拟合
查看>>
找出数组中两数之和为指定值的所有整数对
查看>>
ubuntu IP 扫描
查看>>
项目架构图,mvc架构图
查看>>
Hadoop开发者第四期
查看>>
资料分享:淘宝主备数据库自动切换机制
查看>>
centos 学习总结
查看>>
找工作开篇
查看>>
HTML中的<audio>和<video>标签讲解
查看>>