当前位置:网站首页>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
}
}
边栏推荐
- ROS knowledge: the calling practice of librviz Library
- Deveco device tool helps openharmony device development
- WC statistics are out of date, and every line of cloc code is valid
- 详解PyQt5信号与槽的关系
- [processes and threads]
- record
- 语音模块:pyttsx变声项目
- 蓝桥杯单片机(一)——关闭外设及熄灭LED
- Leetcode 1209. 删除字符串中的所有相邻重复项 II(牛逼,终于过了)
- halcon原理:一维函数function_1d类型【1】
猜你喜欢

如何卸载Gazebo与重装

『忘了再学』Shell流程控制 — 39、特殊流程控制语句

Want to learn ETS development? Teach you to develop an iq-eq test application

Go 语言使用 MySQL 的常见故障分析和应对方法

DuPont analysis: what is the investment value of Anyang Iron and Steel Co., Ltd?

Meta said that the UK security law may "scan all private information" or infringe privacy

PPT制作3D旋转动画从入门到进阶

Getting started with redis - Chapter 4 - data structures and objects - jump table

The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year

mysql,如何在使用存储过程计算最大值
随机推荐
国产化信息 | 爱可生与中科方德完成产品兼容互认证
go-zero微服务实战系列(六、缓存一致性保证)
QT knowledge: QT widgets widget class [01]
2022工具钳工(初级)考试练习题模拟考试平台操作
[processes and threads]
QT knowledge: detailed explanation of view frame qgraphicswidget
Leetcode 1209. 删除字符串中的所有相邻重复项 II(初版本没过)
QT知识:Qt Widgets小部件类【01】
基本数据类型和对应的包装类
使用单调栈解题
Easy to understand soft route brushing tutorial
Voice module: pyttsx sound change project
Want to learn ETS development? Teach you to develop an iq-eq test application
简单易懂的软路由刷机使用教程
ROS2知识(1):开始实践机器人
[no title] 2022 pressure pipeline patrol inspection and maintenance test questions and online simulation test
QT5知识:QT绘制图形
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
Invalid Navicat scheduled task
DuPont analysis: what is the investment value of Anyang Iron and Steel Co., Ltd?