当前位置:网站首页>PAT甲级:1020 Tree Traversals
PAT甲级:1020 Tree Traversals
2022-08-02 03:27:00 【正在黑化的KS】
题目描述:
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 7Sample Output:
4 1 6 3 5 7 2代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
题目大意:给定一棵树的后序遍历和中序遍历 输出这棵树的层序遍历
解题思路:
题目的突破口是:一棵树或者一棵子树的根节点一定在后序遍历数组中的最后一个位置
所以我们可以选确定当前树的根节点是什么数字 再确定该数字在中序遍历中的下标 然后再分左右子树分别递归即可 注意中序遍历的左子树应该是inoder[:index_root],右子树应该是inorder[index_root+1:]. 而后序遍历的左右子树可以通过中序遍历的子树长度求出 因为后序遍历和中序遍历的左子树和右子树的长度应该是对应相等的
最后从根节点bfs一遍即可求出层序遍历的结果
class TreeNode(object) :
def __init__(self,x) :
self.val = x
self.left = None
self.right = None
def buildTree(posto,ino) :
if not posto : return None
root = TreeNode(posto[-1]) # #根节点为后序遍历的末位
root_index = ino.index(posto[-1]) # 确定中序遍历中,根节点的下标
# 根据根节点下标设立中序遍历的左右子树
lefti = ino[:root_index]
righti = ino[root_index+1:]
length = len(lefti) # 确定左子树长度 关键点:后序遍历和中序遍历的子树长度应该相同
# 根据中序遍历左子树长度确定后序遍历的左右子树
leftp = posto[:length]
rightp = posto[length:-1]
root.left = buildTree(leftp,lefti)
root.right = buildTree(rightp,righti)
return root # 每个节点的左右子节点应该是子树的根节点
def bfs(root) : # 输出层序遍历
if root == None : return
queue = [root]
head = 0
while queue :
print(queue[head].val)
if queue[head].left != None : queue.append(queue[head].left)
if queue[head].right != None : queue.append(queue[head].right)
queue.pop(0)
n = int(input())
posto = list(map(int,input().split())) # 后序遍历
ino = list(map(int,input().split())) # 中序遍历
root = buildTree(posto,ino)
bfs(root)边栏推荐
- 面试知识点整理:Skia 架构的场景渲染
- Glide使用及原理分析
- kotlin语法总结(二)
- SATA M2 SSD 无法安装系统的解决方法
- 备战金九银十:Android 高级架构师的学习路线及面试题分享
- The first time to tear the code by hand, how to solve the problem of full arrangement
- VS2017报错:LNK1120 1 个无法解析的外部命令
- (2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets
- Debian 12 Bookworm 尝鲜记
- 大厂底层必修:“应用程序与 AMS 的通讯实现”
猜你喜欢

svg图片实战:自定义view打造中国地图

redis未授权访问(4-unacc)

会计账簿、会计账簿概述、会计账簿的启用与登记要求、会计账簿的格式和登记方法

考(重点理解哪些属于其他货币资金)、其他货币资金的内容、其他货币资金的账务处理(银行汇票存款、银行本票存款、信用卡存款、信用证保证金存款、存出投资款、外埠存款)

解密:链动2+1的商业模式

(2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets

Command Execution Vulnerability

The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)

The CTF introductory notes of SQL injection

浅谈性能优化:APP的启动流程分析与优化
随机推荐
Go Build报错汇总(持续更新)
Binder机制详解(一)
The CTF introductory notes of SQL injection
元宇宙是一个炒作的科幻概念,还是互联网发展的下半场?
管理会计(对内)指引、管理会计要素及其具体内容(可能考,考前记一下,推荐记一下四个大点即可)、
C language uses stack to calculate infix expressions
The CTF introduction of PHP file contains
Mysql创建索引
深入了解为何面试官常说:你还没准备好,我不会录用你
如何一步一步的:玩转全民拼购!
uniapp | Problems with the use of the official map component
Windows下MySQL数据库报“ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:8000‘ (10061)”错误解决
kotlin语法总结(二)
The first time to tear the code by hand, how to solve the problem of full arrangement
元宇宙:为何互联网大佬纷纷涉足?元宇宙跟NFT是什么关系?
管理node版本的工具volta
会计账簿、会计账簿概述、会计账簿的启用与登记要求、会计账簿的格式和登记方法
关于我的项目-微信小程序2(uniapp->wx小程序)
OPENSSL基本实验以及OPENSSL详解
重点考:金融资产概述、交易性金融资产的概念、交易性金融资产的账务处理(取得、持有。出售)、