1020 Tree Traversals (25)(25 point(s))
problem
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
tip
给出后序与中序遍历,要求层次遍历。
answer
#include<iostream>
#include<queue>
#define Max 33
using namespace std;
int n, back[Max], mid[Max];
//int tree[Max];
struct Node{
int d;
Node *l;
Node *r;
};
Node *root;
void Print(){
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node *t = q.front(); q.pop();
if(!t->d) continue;
if(!t->l && !t->r && q.empty()) cout<<t->d;
else cout<<t->d<<" ";
if(t->l) q.push(t->l);
if(t->r) q.push(t->r);
}
// cout<<endl;
}
Node* DFS(int left, int right, int midLeft, int midRight){
if(left > right) return NULL;
Node *a = new Node();
int num = back[right];
int numIndex = -1;
for(int i = midLeft; i <= midRight; i++){
if(mid[i] == num) numIndex = i;
}
int lNum = numIndex - midLeft, rNum = midRight - numIndex;
a->d = num;
Print();
a->l = DFS(left, left + lNum -1, numIndex - lNum, numIndex-1);
a->r = DFS(right-rNum, right-1, numIndex+1, numIndex+rNum);
return a;
}
int main(){
// freopen("test.txt", "r", stdin);
ios::sync_with_stdio(false);
cin>>n;
for(int i = 0; i < n; i++){
cin>>back[i];
}
for(int i = 0; i < n; i++){
cin>>mid[i];
}
root = new Node();
root = DFS(0, n-1, 0, n-1);
Print();
return 0;
}
exprience
- 二叉树要多写几次,就熟悉了。
1020 Tree Traversals (25)(25 point(s))的更多相关文章
- 03-树3 Tree Traversals Again(25 point(s)) 【Tree】
03-树3 Tree Traversals Again(25 point(s)) An inorder binary tree traversal can be implemented in a no ...
- 03-树3 Tree Traversals Again(25 分)
题目 链接 分析 push是二叉树前序遍历的结果,pop是二叉树中序遍历的结果,所以这个题就是已知前序遍历和中序遍历,求后序遍历. AC代码 #include "bits/stdc++.h& ...
- 03-树3 Tree Traversals Again (25 分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- 03-树3 Tree Traversals Again (25 分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- 1086 Tree Traversals Again (25 分)(二叉树的遍历)
用栈来模拟一棵二叉树的先序遍历和中序遍历过程,求这棵二叉树的后序遍历 由题棵知道:push是先序遍历 pop是中序遍历 #include<bits/stdc++.h> using name ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
随机推荐
- 小程序web-view wx.miniProgram.postMessage 坑记录
web-view吧,其实微信官方应该是非常不支持在小程序上嵌套web的,它希望你直接用小程序上的代码,而放弃web的实现,当然,也是为了防止用小程序去嵌套别的广告页面.所以官方对web-view的操作 ...
- Tomcat与Spring中的事件机制详解
最近在看tomcat源码,源码中出现了大量事件消息,可以说整个tomcat的启动流程都可以通过事件派发机制串起来,研究透了tomcat的各种事件消息,基本上对tomcat的启动流程也就有了一个整体的认 ...
- vue组件间通信
组件间通信(父子,兄弟) 相关链接\组件通信http://www.cnblogs.com/xulei1992/p/6121974.html 学习链接Vue.js--60分钟快速入门http://www ...
- 收集了一些python的文章
来自: 戴铭 2010-08-31 17:52:31 newthreading - safer concurrency for Python 安全并发(1回应) http://www.starming ...
- 20165329 Java实验二:面向对象编程
实验内容: 面向对象程序设计-1 实验要求: 提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的截图 实验步骤: 1.按照老师博客的要求新建一个MyUtil项目 在src内新建ja ...
- required_new spring事务传播行为无效碰到的坑!
在测试事务传播行为的时候,因为用了同一个service中的方法测试,所以不管怎么设置都无效了: 原因是aop动态代理只会拦截一次执行方法,第二个方法是照搬的,只要调用其他service中的事务方法,传 ...
- 【算法学习】有旋treap
treap是平衡树的一种.与其他平衡树一样,它也能够支持插入和删除,求第k极值等,接下来我们主要探讨有旋treap的实现过程. treap中每个节点要维护其值,左右孩子以及子树大小.父亲要不要写则看你 ...
- java map遍历并删除特定值
删除map中包含password和username的键值对 若是在map中直接删除,会指针错误 Iterator<Map.Entry<String,Object>> it = ...
- webpack轻松入门教程
webpack之傻瓜式教程及前端自动化入门 接触webpack也有挺长一段时间了,公司的项目也是一直用着webpack在打包处理,但前几天在教新人的情况下,遇到了一个问题,那就是:尽管网上的webpa ...
- java基础63 JavaScript中的Number、Math、String、Date对象(网页知识)
本文知识点(目录): 1.Number对象 2.Math对象 3.String对象 4.Date对象 (日历例子) 1.Number对象 1.1.Number对象的创建方式 方式1: ...