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:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

分析:考察树的建立和遍历。

课参考《编程之美》3.9

#include<iostream>
#include<map>
#include<vector>
#include<queue>
using namespace std; struct Node{
Node *left;
Node *right;
int value;
Node():left(NULL),right(NULL){}
}; void Rebuild(int * PostOrder, int * InOrder, int len, Node* &root){
//判断何时结束递归
if(PostOrder == NULL || InOrder == NULL)
{
root = NULL;
return ;
}
if(root == NULL) root = new Node;
root->value = *(PostOrder + len - 1);
root->left = NULL;
root->right = NULL;
if(len == 1)
return; int count = 0;
int *temp = InOrder;
while(*temp != *(PostOrder + len -1))
{
count ++;
temp++;
if(count > len) break;
}
int left = temp - InOrder ;
int right = len - left - 1;
if(left > 0)
Rebuild(PostOrder, InOrder, left, root->left);
if(right > 0)
Rebuild(PostOrder + left, InOrder+left+1, right, root->right);
} int main()
{
int n,i,t;
while(cin>>n)
{
int *PostOrder = new int[n];
int *InOrder = new int[n];
for(i=0; i<n; i++)
cin>>PostOrder[i];
for(i=0; i<n; i++)
cin>>InOrder[i]; Node *root = new Node;
int post_start,in_start;
post_start = 0;
in_start = 0;
Rebuild(PostOrder, InOrder, n, root);
queue<Node *> q;
q.push(root);
int flag = 1; while(!q.empty()){
if(q.front()->left != NULL)
q.push(q.front()->left);
if(q.front()->right != NULL)
q.push(q.front()->right);
if(flag != n)
cout<<q.front()->value<<" ";
else
cout<<q.front()->value;
flag ++;
q.pop();
} cout<<endl;
}
return 0;
}

【PAT】1020. Tree Traversals (25)的更多相关文章

  1. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  2. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  3. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  4. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  6. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  7. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  8. PAT (Advanced Level) 1020. Tree Traversals (25)

    递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...

  9. 1020. Tree Traversals (25)

    the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...

随机推荐

  1. POJ 2594 Treasure Exploration (可相交最小路径覆盖)

    题意 给你张无环有向图,问至少多少条路径能够覆盖该图的所有顶点--并且,这些路径可以有交叉. 思路 不是裸的最小路径覆盖,正常的最小路径覆盖中两个人走的路径不能有重复的点,而本题可以重复. 当然我们仍 ...

  2. 如何添加真机调试的iOS设备

    注意点: 有时需要同意协议什么的,很多时候刷新出来都是白屏,解决办法: 对于不能确认新协议的问题,我发现了一个解决方法:登陆后,直接在浏览器的地址框访问:https://developer.apple ...

  3. HDU 5360 Hiking 登山 (优先队列,排序)

    题意: 有n个人可供邀请去hiking,但是他们很有个性,每个人都有个预期的人数上下限[Li,Ri],只有当前确定会去的人数在这个区间内他才肯去.一旦他答应了,无论人数怎样变更,他都不会反悔.问最多能 ...

  4. Servlet 3.0 之@WebFilter怎么控制多个filter的执行顺序

    之前我们控制多个filter的执行顺序是通过web.xml中控制filter的位置来控制的,放在上面的会比放在下面的先执行,如下“用户登录检查过滤器”会比“接口日志过滤器”先执行   <!-- ...

  5. session_start保存的客户端cookie的值什么时候改变

    //cookie记录的session_id立刻改变了session_start();echo "old:".session_id();session_regenerate_id() ...

  6. php redis扩展

    安装redis扩展,一定要弄清楚自己的php版本 echo phpinfo(); 查看php信息. 页面搜索Compiler,可以获取自己的VC版本

  7. 【转】Git连接oschina管理代码版本

    原文网址:http://blog.csdn.net/liukang325/article/details/24051467 工作中一般都是用的SVN,最近好像GitHub有些火,看到开源中国上也有Gi ...

  8. xampp 提示 This setting can be configured in the file "httpd-xampp.conf".

    错误提示如下: New XAMPP security concept: Access to the requested object is only available from the local ...

  9. Provider 错误 '80004005' 未指定的错误 的最终解决方法

    今天在配置公司的香港WEB服务器Server2003系统,建好应用程序池后,发现远行程序经常出现下面的错误,刷新几下又可以,但过不了多久又是出现下面的错误!! 在网上查找相关问题得知,这是2003SP ...

  10. python27+django数据库配置常见问题

    mysql缺乏模块,需要安装,建议去http://sourceforge.net/projects/mysql-python/files/mysql-python/下源码编译,或者安装msi文件htt ...