当前位置:网站首页>10-- 根据中序遍历和后序遍历,构造二叉树
10-- 根据中序遍历和后序遍历,构造二叉树
2022-06-23 11:49:00 【JH_Cao】
思路如下注释
// 中序: [4, 1, 5, 3, 6, 2, 7] --> 记录到字典: [[4: 0], [1: 1], [5: 2],[3: 3],[6: 4],[2: 5],[7: 6]]
// 后序: [4, 5, 1, 6, 7, 2, 3]
对后序数组,从后往前
0. curIndex = 6
1. 当取3的时候, index => dict[3] = 3 , 把curIndex = 5
2.那么当前根节点的后继节点, 为index + 1 = 4
那么当前根节点的前驱节点, 为index - 1 = 2
3. 先求当前根节点右边 , 再求左边,递归调用
4. 当left > right 不符合
5. 其中第二步,递归进来的时候,参数左为4,参数右为6
rootVal = 2
rootNode = TreeNode(rootVal)
新的根节点为 2, 从字典中取index --> dict[2] = 5, 把curIndex = 4
右边值: TreeNode(7) left = (index + 1) == 6, right = 6
左边值: TreeNode(6) left = 4, right = (curIndex - 1) == 4
方法总结
1. 中序遍历用来生成字典
2. 后序遍历,从后往前,生成右,左节点,递归调用
/**
// 中序: 左- 根 - 右
// 后序: 左- 右 - 根
举例:
----------------------------------
3
1 2
4 5 6 7
// 中序: [4, 1, 5, 3, 6, 2, 7] --> 记录到字典: [[4: 0], [1: 1], [5: 2],[3: 3],[6: 4],[2: 5],[7: 6]]
// 后序: [4, 5, 1, 6, 7, 2, 3]
对后序数组,从后往前
0. curIndex = 6
1. 当取3的时候, index => dict[3] = 3 , 把curIndex = 5
2.那么当前根节点的后继节点, 为index + 1 = 4
那么当前根节点的前驱节点, 为index - 1 = 2
3. 先求当前根节点右边 , 再求左边,递归调用
4. 当left > right 不符合
5. 其中第二步,递归进来的时候,参数左为4,参数右为6
rootVal = 2
rootNode = TreeNode(rootVal)
新的根节点为 2, 从字典中取index --> dict[2] = 5, 把curIndex = 4
右边值: TreeNode(7) left = (index + 1) == 6, right = 6
左边值: TreeNode(6) left = 4, right = (curIndex - 1) == 4
------------------
总结:
1. 中序遍历用来生成字典
2. 后序遍历,从后往前,生成右,左节点,递归调用
*/
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
}
}
边栏推荐
- 并购增资或将有望启动东软越通新动能?
- MySQL matches multiple values in one field
- Does the inductance have polarity?
- @Dark horse fans, haven't you received this "high temperature subsidy"?
- wc 统计已过时,cloc 每一行代码都有效
- Tamidog | analysis of investor types and enterprise investment types
- Vous comprenez vraiment la capacité de sortie de LDO!?
- Learning notes sweep crawler framework
- 全国进入主汛期,交通运输部:不具备安全运行条件的线路坚决停运!
- 语音数据标注工具与平台
猜你喜欢

单向链表实现--计数

Comment Huawei Cloud réalise l'architecture mondiale de réseau audio - vidéo en temps réel à faible latence

Meta said that the UK security law may "scan all private information" or infringe privacy
![[cloud native & microservice viii] source code analysis of weightedresponsetimerule of ribbon load balancing strategy (response time weighting)](/img/cc/afad5b5d327416c809be3b661bcc13.png)
[cloud native & microservice viii] source code analysis of weightedresponsetimerule of ribbon load balancing strategy (response time weighting)

16路HD-SDI光端机多路HD-SDI高清视频光端机16路3G-SDI高清音视频光端机

简单易懂的软路由刷机使用教程

Redis 入门-第四篇-数据结构与对象-跳跃表

想学习eTS开发?教你开发一款IQ-EQ测试应用
![[processes and threads]](/img/6b/ff1076a3809ecc412f2bf6517c278e.png)
[processes and threads]

股权转让热点:重庆建科建设工程质量检测有限公司93.75%股权转让
随机推荐
汉源高科1路非压缩4K-DVI光端机4K高清非压缩DVI转光纤4K-DVI高清视频光端机
Introduction to redis - Chapter 1 - data structures and objects - simple dynamic string (SDS)
More than observation | Alibaba cloud observable suite officially released
Does the inductance have polarity?
学习笔记 scrapy 爬虫框架
汉源高科1路千兆光口转4路千兆以太网电口千兆1光4电光纤收发器
The computer broke down. I changed the computer. There was a problem installing the node environment. The URL is not defined
某问答社区App x-zse-96签名分析
汉源高科USB2.0光端机USB2.0光纤延长器USB2.0光纤传输器USB2.0接口转光纤
坦然面对未来,努力提升自我
Where to find capacitance parameters!?
在flinksql中 kafka流表跟mysql 纬度流表做left join,根据I’d做关联,假
Ppt makes 3D rotation animation from beginner to advanced
你真的理解LDO的輸出電容嗎!?
Deveco device tool helps openharmony device development
32路电话+2路千兆以太网32路PCM电话光端机支持FXO口FXS语音电话转光纤
Leetcode 1209. 删除字符串中的所有相邻重复项 II(初版本没过)
【云驻共创】华为云HCIA-IoT V2.5培训系列内容之物联网概览
【云舟说直播间】-数字安全专场明天下午正式上线
Daily question 7-1652 Defuse the bomb