当前位置:网站首页>10-- construct binary tree according to middle order traversal and post order traversal
10-- construct binary tree according to middle order traversal and post order traversal
2022-06-23 12:14:00 【JH_ Cao】
The idea is as follows
// Middle preface : [4, 1, 5, 3, 6, 2, 7] --> Record in the dictionary : [[4: 0], [1: 1], [5: 2],[3: 3],[6: 4],[2: 5],[7: 6]]
// In the following order : [4, 5, 1, 6, 7, 2, 3]
For the subsequent array , From back to front
0. curIndex = 6
1. When taking 3 When , index => dict[3] = 3 , hold curIndex = 5
2. Then the successor nodes of the current root node , by index + 1 = 4
The predecessor node of the current root node , by index - 1 = 2
3. First find the right side of the current root node , Find the left side again , Recursively call
4. When left > right Do not conform to the
5. The second step , When recursion comes in , The left of the parameter is 4, The right side of the parameter is 6
rootVal = 2
rootNode = TreeNode(rootVal)
The new root node is 2, From the dictionary index --> dict[2] = 5, hold curIndex = 4
The right value : TreeNode(7) left = (index + 1) == 6, right = 6
Left value : TreeNode(6) left = 4, right = (curIndex - 1) == 4
Methods to summarize
1. Middle order traversal is used to generate dictionaries
2. After the sequence traversal , From back to front , Generate right , The left node , Recursively call
/**
// Middle preface : Left - root - Right
// In the following order : Left - Right - root
give an example :
----------------------------------
3
1 2
4 5 6 7
// Middle preface : [4, 1, 5, 3, 6, 2, 7] --> Record in the dictionary : [[4: 0], [1: 1], [5: 2],[3: 3],[6: 4],[2: 5],[7: 6]]
// In the following order : [4, 5, 1, 6, 7, 2, 3]
For the subsequent array , From back to front
0. curIndex = 6
1. When taking 3 When , index => dict[3] = 3 , hold curIndex = 5
2. Then the successor nodes of the current root node , by index + 1 = 4
The predecessor node of the current root node , by index - 1 = 2
3. First find the right side of the current root node , Find the left side again , Recursively call
4. When left > right Do not conform to the
5. The second step , When recursion comes in , The left of the parameter is 4, The right side of the parameter is 6
rootVal = 2
rootNode = TreeNode(rootVal)
The new root node is 2, From the dictionary index --> dict[2] = 5, hold curIndex = 4
The right value : TreeNode(7) left = (index + 1) == 6, right = 6
Left value : TreeNode(6) left = 4, right = (curIndex - 1) == 4
------------------
summary :
1. Middle order traversal is used to generate dictionaries
2. After the sequence traversal , From back to front , Generate right , The left node , Recursively call
*/
class xiapi10 {
var myPostorder = [Int]()
var curIndex = 0
var dict = [Int: Int]()
func buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode10? {
myPostorder = postorder
curIndex = myPostorder.count - 1
for index in 0..<inorder.count {
dict[inorder[index]] = index
}
return getNodeByLeftAndRight(0, myPostorder.count - 1)
}
func getNodeByLeftAndRight(_ left: Int, _ right: Int) -> TreeNode10? {
if left > right {
return nil
}
let rootVal = myPostorder[curIndex]
let rootNode = TreeNode10(rootVal)
guard let index = dict[rootVal] else {
return nil
}
curIndex -= 1
rootNode.right = getNodeByLeftAndRight(index + 1, right)
rootNode.left = getNodeByLeftAndRight(left, index - 1)
return rootNode
}
}
class TreeNode10 {
var val: Int
var left: TreeNode10?
var right: TreeNode10?
init() {
self.val = 0
self.left = nil
self.right = nil
}
init(_ val: Int) {
self.val = val
self.left = nil
self.right = nil
}
init(_ val: Int, _ left: TreeNode10?, _ right: TreeNode10?) {
self.val = 0
self.left = left
self.right = right
}
}
边栏推荐
- Three ways to learn at work
- record
- Easy to understand soft route brushing tutorial
- Oversampling series I: sampling theorem and oversampling rate
- 2022 construction worker - Equipment direction - post skill (construction worker) test question simulation test platform operation
- ROS察微【57】:配置手臂机器人来抓东西
- ROS observation [57]: configure arm robots to grasp things
- 凭借32量子比特!Rigetti Computing打入英国量子计算市场
- 【无标题】2022年压力管道巡检维护试题及在线模拟考试
- Necessary software for automation or electrical specialty
猜你喜欢

Open classes are short videos! Tonight, I will teach you how to realize accurately!

华为云GaussDB重磅发布HTAP商用,定义云原生数据库2.0新范式

Redis 入门-第二篇-数据结构与对象-链表

详解PyQt5信号与槽的关系

DevEco Device Tool 助力OpenHarmony设备开发

利用XtraDiagram.DiagramControl进行流程图形的绘制和控制

halcon原理:一维函数function_1d类型【1】

Runtime application self-protection (rasp): self-cultivation of application security

想学习eTS开发?教你开发一款IQ-EQ测试应用

【零基础微信小程序】基于百度大脑人像分割的证件照换底色小程序实战开发
随机推荐
Ros2 knowledge (6): principle and practice of coordinate object TF
Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
【基础知识】~ 数据位宽转换器
Meta said that the UK security law may "scan all private information" or infringe privacy
2022施工员-装饰方向-岗位技能(施工员)操作证考试题库模拟考试平台操作
Go zero micro Service Practice Series (VI. cache consistency assurance)
I am in Foshan. Where can I open an account? Is it safe to open a mobile account?
Redis 入门-第一篇-数据结构与对象-简单动态字符串(SDS)
基本数据类型和对应的包装类
记录
Where to find capacitance parameters!?
ROS2知识(1):开始实践机器人
go-zero微服务实战系列(六、缓存一致性保证)
QT5知识:QT绘制图形
Voice module: pyttsx sound change project
ROS knowledge: the calling practice of librviz Library
学习笔记 scrapy 爬虫框架
利用XtraDiagram.DiagramControl进行流程图形的绘制和控制
ROS知识:librviz库的调用实践
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案