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

本题考查树的遍历, 给出后根遍历和中根遍历,可以确定一棵树, 最后使用层次遍历获取LevelOrder,思路如下:

直接上例子, 题目给出后根遍历2 3 1 5 7 6 4, 中根遍历1 2 3 4 5 6 7

后跟遍历可知, 4是整棵树的跟, 再看中跟遍历中4的位置,把整棵树分成1,2,3组成的左子树和5,6, 7组成的右子树, 同理, 后根遍历中也会分成2,3,1 和5,7,6两颗子树, 此时,后根2,3,1对应中根1 2 3, 后根5,7,6对应中跟5,6,7可继续递归求解,但是要注意判断特殊情况:

  • 确定根后, 发现根在中根遍历的最右边,那么此书右子树为空
  • 确定根后, 发现根在中根遍历的最左边,那么此书左子树为空
#include <iostream>
#include <queue>
using namespace std; typedef struct
{
int value;
int left;
int right;
} Node; Node node[32];
int postOrder[32];//后跟遍历
int midOrder[32];//中跟遍历
int N; //递归函数, l1,r1表示后跟遍历的左界和右界
//l2, r2表示中根遍历的左界和右界
void f(int l1, int r1, int l2, int r2)
{
int root = postOrder[r1]; if(l1 == r1)
{
node[root].left = node[root].right = -1;
return;
}
//i记录根在midOrder的位置
int i = 0;
while(midOrder[i] != root) i++;
int cntL = i - l2; //cntL表示i左边有几个元素,用于分隔postOrder if(i == l2)//左子树为空情况
{
node[root].left = -1;
node[root].right = postOrder[r1 - 1];
f(l1 + cntL, r1 - 1, i + 1, r2);
return;
}
if(i == r2)//右子树为空情况
{
node[root].right = -1;
node[root].left = postOrder[r1 - 1];
f(l1, l1 + cntL - 1, l2, i - 1);
return;
}
//两边都不为空,先求解两根
f(l1, l1 + cntL - 1, l2, i - 1);
f(l1 + cntL, r1 - 1, i + 1, r2); //最后给根赋值
node[root].left = postOrder[l1 + cntL - 1];
node[root].right = postOrder[r1 - 1]; }
//层次遍历
void bfs()
{
int root = postOrder[N - 1];
queue<int> q;
q.push(root);
int first = 1;
while(!q.empty())
{
int v = q.front();
if(first)
{
first = 0;
cout << v;
}
else
{
cout << " " << v;
}
q.pop();
if(node[v].left != -1)
q.push(node[v].left);
if(node[v].right != -1)
q.push(node[v].right);
}
}
int main()
{
cin >> N; for(int i = 0; i < N; i++)
{
cin >> postOrder[i];
}
for(int i = 0; i < N; i++)
{
cin >> midOrder[i];
} f(0, N - 1, 0, N - 1); bfs(); return 0;
}

1020. Tree Traversals的更多相关文章

  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[二叉树遍历]

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

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

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

  4. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

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

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

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

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

  7. PAT Advanced 1020 Tree Traversals (25 分)

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

  8. 1020 Tree Traversals——PAT甲级真题

    1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...

  9. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  10. 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. javascript深入理解js闭包(转载)

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...

  2. SQL Sever数据库的基本操作和它的建立

    SQL数据库: 1.数据库概述 (1) 用自定义文件格式保存数据的劣势. (2) DBMS(DataBase Management System,数据库管理系统)和数据库,平时谈到"数据库& ...

  3. 关于label和input对齐的那些事

    input文本和label对齐 默认状态下,也就是下面这样, 文字和input是居中的. <div> <label>我是中国人</label> <input ...

  4. 通过 Composer Github Packagist制作发布共享PHP包

    参考来源: https://laravel-china.org/topics/1002 https://rivsen.github.io/post/how-to-publish-package-to- ...

  5. [OpenGL] mac上运行NateRobin的OpenGL教程找不到 data file 解决方案

    之前买的OpenGL编程指南第七版一直没看,最近开始看了,然后按照教程推荐的去指定网址下载NateRobin的OpenGL教程,但发现网址已经提示Error:404了, 然后谷歌搜索到可用的下载网址为 ...

  6. Mybatis的@Options注解

    mybatis的@Options注解能够设置缓存时间,能够为对象生成自增的key 第一个使用场景: 有一个表 CREATE TABLE instance ( instance_id BIGINT UN ...

  7. TCP/IP笔记(三)数据链路层

    数据链路的作用 数据链路层的协议定义了通过通信媒介互连的设备之间传输的规范.通信媒介包括双绞线电缆.同轴电缆.光纤.电波以及红外线等介质.此外,各个设备之间有时也会通过交换机.网桥.中继器等中转数据. ...

  8. 初识 Javascript.01 -- Javascript基础|输出方式、变量、变量命名规范、数据类型、

    Javascript基础 1 聊聊Javascript 1.1 Javascript的历史来源 94年网景公司   研发出世界上第一款浏览器. 95年 sun公司   java语言诞生 网景公司和su ...

  9. CSS.02 -- 样式表 及标签分类(块、行、行内块元素)、CSS三大特性、背景属性

    样式表书写位置  内嵌式写法 <head> <style type="text/css"> 样式表写法 </style> </head&g ...

  10. datagrid-detailview.js easyui表格嵌套

    datagrid-detailview.js easyui表格嵌套