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 分)的更多相关文章

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

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

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

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

  3. PAT Advanced 1020 Tree Traversals (25 分)

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

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

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

  5. 1020 Tree Traversals (25分)思路分析 + 满分代码

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

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

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

  7. 1020. Tree Traversals (25)

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

  8. 1020. Tree Traversals (25) -BFS

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

  9. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

随机推荐

  1. 01 搭建EasyMock环境

    EasyMock 介绍 EasyMock是一个构建模拟数据的平台,也可以说是一个在线mockJs平台 EasyMock优势 省去配置.安装mockJs步骤,解决多人协作Mock数据不互通问题 不需要在 ...

  2. 登录页面判断session退出登录清空session

    1 2 3 4 5 6 if(empty($_POST)){      $_SESSION = array();     if(isset($_COOKIE[session_name()])){    ...

  3. 深度学习与人类语言处理-语音识别(part1)

    语音识别 语音识别该何去何从? 1969年,J.R. PIERCE:"语音识别就像把水变成汽油.从大海中淘金.治疗癌症.人类登陆月球" 当然,这是50年前的想法,那么语音识别该如何 ...

  4. [转] SQL Server 数据库性能优化

    分析比较执行时间计划读取情况 1. 查看执行时间和cpu set statistics time on select * from Bus_DevHistoryData set statistics ...

  5. [日志分析]Graylog2采集Nginx日志 主动方式

    这次聊一下Graylog如何主动采集Nginx日志,分成两部分: 介绍一下 Graylog Collector Sidecar 是什么 如何配置 Graylog Collector Sidecar 采 ...

  6. Docker Compose 文件讲解

    Docker Compose 是什么 官方文档: Docker Compose是定义和运行多容器 Docker 应用程序的工具.使用"Compose",您可以使用 YAML 文件来 ...

  7. 数据挖掘入门系列教程(五)之Apriori算法Python实现

    数据挖掘入门系列教程(五)之Apriori算法Python实现 加载数据集 获得训练集 频繁项的生成 生成规则 获得support 获得confidence 获得Lift 进行验证 总结 参考 数据挖 ...

  8. ESLint {} 内部强制有空格 .eslintrc.js 配置文件 'object-curly-spacing': ["error", "always"],

    module.exports = { root: true, 'extends': [ 'plugin:vue/essential', '@vue/standard' ], rules: { 'obj ...

  9. Git 基础入门

    目录 git安装 基本设置 创建版本库 相关概念 将代码提交到分支仓库 版本回退 代码修改 撤销修改 文件删除 github 远程仓库 添加远程仓库 克隆远程仓库 分支操作 忽略特殊文件 git安装 ...

  10. AndroiBugs Android漏洞扫描器

    Download Address:https://github.com/AndroBugs/AndroBugs_Framework Usage for Unix/Linux: ####To run t ...