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 ...
随机推荐
- 20155201 2016-2017-2 《Java程序设计》第六周学习总结
20155201 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 第十章 输入/输出 字节输入类: Java将输入/输出抽象化为串流,数据有来源及目的地,衔接 ...
- vue引入jquery的方法
1.局部引入 通过命令下载jquery npm install jquery --save-dev 在需要引入jquery的组件中通过import $ from 'jquery'引入即可 2.全局 ...
- 多校 HDU 6397 Character Encoding (容斥)
题意:在0~n-1个数里选m个数和为k,数字可以重复选: 如果是在m个xi>0的情况下就相当于是将k个球分割成m块,那么很明显就是隔板法插空,不能为0的条件限制下一共k-1个位置可以选择插入隔板 ...
- Oracle Certified Java Programmer 经典题目分析(一)
Given: 1. public class returnIt { 2. returnType methodA(byte x, double y){ 3. return (short) x/y * 2 ...
- 【逆向知识】开发WinDBG扩展DLL
如何开发WinDbg扩展DLL WinDbg扩展DLL是一组导出的回调函数,用于实现用户定义的命令.以便从内存转储中提取特定的信息.扩展dll由调试器引擎加载,可以在执行用户模式或内核模式调试时提供自 ...
- 从Python到Web开发
基础部分: 1-编程基础及Python环境部署 2-Python基础语法-内存管理-运算符-程序控制 3-Python内置结构-列表 4-Python数据类型之元组-字符串 5-python的封装与结 ...
- 3->集群架构主机克隆教程
centos7系统集群主机克隆: 有道笔记链接地址
- windows7+cuda8+cudnn6+python36+tensorflow_gpu1.4配置
下载文件 cuda8,自行网上下载online的安装包就好了 cudnn6 python36 tensorflow_gpu 下载地址:https://pan.baidu.com/s/1mjwOi5E ...
- python中的多进程
具体参考这个博客地址:http://www.cnblogs.com/lxmhhy/p/6052167.html
- ORA-12514: TNS:listener does not currently know of service …
问题描述: 今天数据库查询时遇到问题,具体情形如下截图所示: 问题分析: 看错误明显是TNS监听有问题,要么配置错了,要么数据库没起来.但是当前数据库起来了,也能正常连接使用,因此 考虑被查询对象可能 ...