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

主要考查,中后遍历转前序,前中遍历建树,层序遍历树
#include <iostream>
#include <vector>
#include <queue> using namespace std; struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
}; vector<int> pre,in,post,ans;
queue<TreeNode*> que; void preOrder(int root,int start,int end){
if(start>end) return;
int i=;
while(i<=end&&in[i]!=post[root]) i++;
pre.push_back(post[root]);
preOrder(root--end+i,start,i-);
preOrder(root-,i+,end);
} TreeNode* buildTree(int root,int start,int end){
if(start>end) return NULL;
int i=;
TreeNode *t=new TreeNode();
while(i<=end&&in[i]!=pre[root]) i++;
t->val=pre[root];
t->left=buildTree(root+,start,i-);
t->right=buildTree(root++i-start,i+,end);
return t;
} void levelOrder(TreeNode *tree){
que.push(tree);
while(!que.empty()){
TreeNode *tmp=que.front();
ans.push_back(tmp->val);
que.pop();
if(tmp->left!=NULL) que.push(tmp->left);
if(tmp->right!=NULL) que.push(tmp->right);
} }
int main()
{
int N;
scanf("%d",&N);
post.resize(N);in.resize(N);
for(int i=;i<N;i++) scanf("%d",&post[i]);
for(int i=;i<N;i++) scanf("%d",&in[i]);
preOrder(N-,,N-);
TreeNode *tree=buildTree(,,N-);
levelOrder(tree);
for(int i=;i<ans.size();i++)
if(i!=ans.size()-) cout<<ans[i]<<" ";
else cout<<ans[i];
system("pause");
return ;
}

查看柳神博客,对代码修改,实际可以建立一个索引,这样就可以直接得到先序遍历的结果:

#include <iostream>
#include <vector>
#include <algorithm> using namespace std;
struct node{
int index;
int val;
};
bool cmp(node n1,node n2){
return n1.index<n2.index;
}
vector<int> in,post;
vector<node> ans; void preOrder(int root,int start,int end,int index){
if(start>end) return;
int i=;
while(i<=end&&in[i]!=post[root]) i++;
ans.push_back({index,post[root]});
preOrder(root--end+i,start,i-,*index+);
preOrder(root-,i+,end,*index+);
}
int main()
{
int N;
scanf("%d",&N);
post.resize(N);in.resize(N);
for(int i=;i<N;i++) scanf("%d",&post[i]);
for(int i=;i<N;i++) scanf("%d",&in[i]);
preOrder(N-,,N-,);
sort(ans.begin(),ans.end(),cmp);
for(int i=;i<N;i++)
if(i!=N-) printf("%d ",ans[i].val);
else printf("%d",ans[i].val);
system("pause");
return ;
}

PAT Advanced 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) [⼆叉树的遍历,后序中序转层序]

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

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

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

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

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

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

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

  7. 1020 Tree Traversals (25 分)

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

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

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

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

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

随机推荐

  1. PL-VIO Docker测试

    Docker里面测试PL-VIO的代码 PL-VIO贺一家大佬最近开源出来的一个点线特征融合的VIO代码,基于Vins-Mono,想要快速测试一下代码,所以就写了个简单的Dockerfile来测试一下 ...

  2. npm run build报错 No PostCSS Config found in

    在项目根目录新建postcss.config.js文件,并对postcss进行配置: module.exports = { plugins: [ require('autoprefixer')//自动 ...

  3. eNSP——Hybrid接口的应用

    原理: Hybrid接口既可以连接普通终端的接入链路又可以连接交换机间的干道链路,它允许多个VLAN的帧通过,并可以在出接口方向将某些VLAN帧的标签剥掉. Hybrid接口处理VLAN帧的过程如下: ...

  4. 通过JS动态追加标签,以父评论子评论为例

    以下代码前后端交互以Django模板语法为例 先来以伪代码来示意用法: HTML部分: JS动态插入部分代码: 运行之后我们来浏览器看检查打印的内容: 看插入前后打印结果我们可以得知 $title[0 ...

  5. shell中变量的测试与替换

    在某些时刻我们经常需要判断某个变量是否存在,若变量存在则使用既有的设置,若变量不存在则给予一个常用的设置. (1) 变量未被设置或者内容为空,则替换为新的内容. new_var=${old_var-c ...

  6. [转帖]中国新超算彻底告别进口CPU 国产芯片已可与国外抗衡

    中国新超算彻底告别进口CPU 国产芯片已可与国外抗衡 蓝天·2017-10-17·本土IC 来源: 观察者网 https://www.laoyaoba.com/html/news/newsdetail ...

  7. Python实现二叉树的非递归先序遍历

    思路: 1. 使用列表保存结果: 2. 使用栈(列表实现)存储结点: 3. 当根结点存在,保存结果,根结点入栈: 4. 将根结点指向左子树: 5. 根结点不存在,栈顶元素出栈,并将根结点指向栈顶元素的 ...

  8. HTTP协议之需要了解的网络基础

    HTTP(超文本传输协议)是应用层协议,构建在TCP/IP之上,主要用来完成客户端和服务端的通信.本文主要介绍一些和HTTP相关的内容. 1. TCP/IP 分为四层: 应用层:HTTP.DNS.FT ...

  9. 缓存穿透 & 缓存雪崩 & 缓存击穿

    一 缓存穿透 1. 行为 查询一个一定不存在的数据.存储层(姑且认为是db,下面都用db指代)查不到数据则不写入缓存,那么下次请求这个不存在的数据同样会到db层查询,失去了缓存的意义.流量大或人为恶意 ...

  10. global和nonlocal的区别

    global可以在任何地方修饰变量,而且被global修饰的变量直接被标识为全局变量,对该变量修改会影响全局变量的值,但不影响函数中未被global修饰的同名变量(依然是局部变量),nonlocal只 ...