当前位置:网站首页>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
}
}
边栏推荐
- [cloud native & microservice viii] source code analysis of weightedresponsetimerule of ribbon load balancing strategy (response time weighting)
- Ros2 knowledge (6): principle and practice of coordinate object TF
- Mobile securities account opening transaction? Is it safe to open an account online now?
- Use xtradiagram Diagramcontrol for drawing and controlling process graphics
- 二维激光SLAM( 使用Laser Scan Matcher )
- Shell process control - 39. Special process control statements
- HMS core video editing service has the ability to open templates, helping users get the same cool video with one click
- 【UVM入门 ===> Episode_7 】~ sequence、sequence item、sequencer、driver
- Redis 入门-第四篇-数据结构与对象-跳跃表
- go-zero微服务实战系列(六、缓存一致性保证)
猜你喜欢

得物多活架构设计之路由服务设计

With 32 qubits! Rigetti computing enters the UK quantum computing market

2D laser Slam (using laser scan matcher)

Shell process control - 39. Special process control statements

使用单调栈解题

股权转让热点:重庆建科建设工程质量检测有限公司93.75%股权转让

Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022

Halcon principle: correlation matching

蓝桥杯单片机(一)——关闭外设及熄灭LED

DevEco Device Tool 助力OpenHarmony设备开发
随机推荐
Open classes are short videos! Tonight, I will teach you how to realize accurately!
Three ways to learn at work
ROS察微【51】:如何将里程计和 IMU 与 robots_localization 融合
10-- 根据中序遍历和后序遍历,构造二叉树
"Dream of children's travel" in 2022, GAC Honda children's road safety charity travel entered the Northeast
杜邦分析法解读:安阳钢铁股份有限公司企业投资价值何在?
LinkedList 5-141. 环形链表
安装Rstudio Desktop和Rstudio Server免费版本
Halcon principle: Auto_ Threshold operator
公开课丨玩的就是短视频!今晚教你精准变现!
Ppt makes 3D rotation animation from beginner to advanced
Introduction to redis - Chapter 1 - data structures and objects - simple dynamic string (SDS)
Oracle数据库的主导地位被云竞争对手逐渐侵蚀
QT knowledge: detailed explanation of view frame qgraphicswidget
DevEco Device Tool 助力OpenHarmony设备开发
Which securities company is the most reliable and safe to open an account
机器学习系列5:距离空间(1)
Ros2 knowledge (1): start practicing robots
年薪中位数超30万,南大AI专业首届毕业生薪资曝光
Redis 入门-第三篇-数据结构与对象-字典