PAT甲 1020 Tree Traversals (树的后序中序->层序)
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 (≤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
题意:根据树的后序中序求树的层序
请先看树的后序中序->前序https://www.cnblogs.com/1013star/p/11569194.html
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5 int post[35],in[35];
6 int a[11000];
7 //root是后序中的当前根的位置,st,ed是该子树在中序遍历中的最左位置和最右位置
8 void dfs(int root,int st,int ed,int index)
9 {
10
11 if(st>ed)
12 return;
13 int i=st;
14 while(in[i]!=post[root])
15 i++;
16 //cout<<post[root]<<" ";
17 // cout<<"index="<<index<<" "<<"root="<<post[root]<<endl;
18 a[index]=post[root];
19 dfs(root-(ed-i+1),st,i-1,index*2);
20 dfs(root-1,i+1,ed,index*2+1);
21 }
22 int main()
23 {
24 int n;
25 cin>>n;
26 for(int i=0;i<n;i++)
27 cin>>post[i];
28 for(int i=0;i<n;i++)
29 cin>>in[i];
30 dfs(n-1,0,n-1,1);
31 int cnt=0;
32 for(int i=1;i<10000;i++)
33 {
34 if(a[i]!=0)
35 {
36 cnt++;
37 if(cnt==n)
38 {
39 cout<<a[i];
40 break;
41 }
42 else
43 cout<<a[i]<<" ";
44 }
45 }
46 return 0;
47 }
原文地址:https://www.cnblogs.com/1013star/p/11569296.html
- 12个非常实用的JavaScript小技巧
- 关于PHP浮点数精度损失问题
- FreeMarker与JSP 2.0 + JSTL组合进行比较
- 从零开始学设计模式(1):基础编程模式
- 机器学习实战 | 第四章:模型验证和选择
- ofbiz中FreeMarkerWorker的makeConfiguration方法
- 后台进程(守护进程)自动备份PostgreSQL数据库
- 了解这些PHP小技巧吗?
- 结构体字节对齐
- 在Atom中设置Python开发环境
- Assignment 3 (神经网络) | 斯坦福CS231n-深度学习与计算机视觉课程
- ofbiz view渲染处理机制
- ofbiz方法一 条件查询createConditionList
- ofbiz的ant命令创建模块
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【python-leetcode295-双堆】数据流的中位数
- join的使用
- C语言之不能在scanf中使用换行
- 聊聊java中的哪些Map:(五)HashTable与HashMap的区别
- paddlepaddle之飞机识别
- c语言之条件编译
- python之使用魔术方法__getitem__和__len__
- B+Tree index structures in InnoDB(7.InnoDB中B+树的索引结构)
- Java基础
- 广度优先遍历--选课的智慧
- paddlepaddle目标检测之水果检测(yolov3_mobilenet_v1)
- 【每日一题】41. First Missing Positive
- springmvc之第一个springmvc程序
- python小例子(一)
- springmvc之RequestMapping注解既可以修饰类也可以修饰方法