1020 Tree Traversals (25 分)
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 (≤), 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
分析:根据后序遍历和中序遍历 输出层序遍历
我的做法是利用后序遍历和中序遍历建树 再层序遍历输出
利用两个变量来记录我们需要建子树的范围 另一个变量记录每层的根节点
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef struct BtNode* Bt;
struct BtNode
{
int Num;
Bt RT;
Bt LT;
};
vector<vector<int> > Order();
Bt BuildTree(Bt T,int i2,int j2,int j1)
{
if (i2 >= j2)
return T;
int i = i2;
for (; i < j2; i++)
{
if (Order[][i] == Order[][j1 - ])
break;
}
if (!T)
{
T = new BtNode();
T->LT = NULL;
T->RT = NULL;
T->Num = Order[][i];
}
T->LT =BuildTree(T->LT,i2,i,j1-j2+i);
T->RT = BuildTree(T->RT,i+,j2,j1-);
return T;
}
int main()
{
int N;
cin >> N;
int num;
for(int i=;i<;i++)
for (int j = ; j < N; j++)
{
cin >> num;
Order[i].push_back(num);
}
Bt T=NULL;
T = BuildTree(T,,N,N);
queue<Bt> Q;
Q.push(T);
cout << T->Num;
while (!Q.empty())
{
if (Q.front()->LT)
{
Q.push(Q.front()->LT);
cout << " " << Q.front()->LT->Num;
}
if (Q.front()->RT)
{
Q.push(Q.front()->RT);
cout << " " << Q.front()->RT->Num;
}
Q.pop();
}
return ;
}
1020 Tree Traversals (25 分)的更多相关文章
- 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 (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- 1020. Tree Traversals (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...
- 1020. Tree Traversals (25) -BFS
题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
随机推荐
- django 从零开始 4 404页面和500页面设置
在视图函数中定义两个 函数 分别对应404 个500页面 (自定义html内容吧,这里只是展示) 在template页面指向自己定义的404.html和500.html页面 在项目的urls中设置 h ...
- 【10】openlayers 视图view
创建地图: //View对象代表地图的简单2D视图 //创建view let view = new ol.View({ center:[109,34],//视图的初始中心 maxZoom:18,//最 ...
- Js数组代替写循环的几个方法
简介 循环是个不可避免的结构,而且不好复用,同时循环还很难加入其他操作中.更麻烦的是,使用循环就意味着在每一个新的迭代中有更多变化需要响应. 上了循环的控制结构会使代码看起来变得复杂,故而这里提几个替 ...
- CrawlSpiders简介
转:https://www.cnblogs.com/ellisonzhang/p/11124516.html#4295547 一.CrawlSpiders类简介 通过下面的命令可以快速创建 Crawl ...
- turtle学习笔记
1.turtle的绘图窗体 turtle.setup(width, height, startx,starty) - setup()设置窗体大小及位置- 4个参数中后两个可选(后两个省略时默认窗口在屏 ...
- python之 filter
filter的语法:filter(函数名字,可迭代的变量) 其实filter就是一个“过滤器”:把[可迭代的变量]中的值,挨个地传给函数进行处理,那些使得函数的返回值为True的变量组成的迭代器对象就 ...
- Natas16 Writeup(正则匹配,php命令执行)
Natas16: 源码如下 <? $key = ""; if(array_key_exists("needle", $_REQUEST)) { $key ...
- Unity 游戏框架:UI 管理神器 UI Kit
UI Kit 快速入门 首先我们来进行 UI Kit 的快速入门 制作一个界面的,步骤如下: 准备 生成代码 逻辑编写 运行 1. 准备 先创建一个场景 TestUIHomePanel. 删除 Hie ...
- 进制-Adding Two Negabinary Numbers
2020-02-20 14:52:41 问题描述: 问题求解: 最开始的想法是将两个数字先转化成自然数在求和,最后转化回去,但是实际上这种方案是不可取的,主要的问题就是会爆掉. 那么就得按位进行运算了 ...
- [离散化+树状数组]CodeForces - 652D Nested Segments
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...