当前位置:网站首页>【二叉树】节点与其祖先之间的最大差值
【二叉树】节点与其祖先之间的最大差值
2022-07-04 22:35:00 【豪冷啊】
0x00 题目
给定二叉树的根节点 root
找出存在于 不同
节点 A
和 B
之间的最大值 V
其中 V = |A.val - B.val|
且 A
是 B
的祖先
如果 A
的任何子
节点之一为 B
或者 A
的任何子
节点是 B
的祖先
那么我们认为 A
是 B
的祖先
粟子:
我们有大量的节点与其祖先的差值,其中一些如下:|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
在所有可能的差值中,最大值 7
由 |8 - 1| = 7
得出
0x01 思路
单看一条路径:8-3-6-4
要计算差值8-3,8-6,8-4
3-6,3-4
6-4
然后在这些差值中
取得最大
的一个差值
要想差值最大
就要在路径中找到最大值
和最小值
所以
每经过一个节点
就可以更新
最大值和最小值
并且求出差值
0x02 解法
语言:Swift
树节点:TreeNode
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
self.right = right
}
}
解法:
func maxAncestorDiff(_ root: TreeNode?) -> Int {
guard let root = root else { return 0 }
var res = Int.min
func find(_ root: TreeNode?, _ maxVal: Int, _ minVal: Int) {
guard let root = root else { return }
// 计算差值
var max1 = abs(maxVal - root.val)
var min1 = abs(minVal - root.val)
// 获取最大差值
let big = max(max1, min1)
// 更新结果
res = max(res, big)
// 更新最大,最小值
max1 = max(maxVal, root.val)
min1 = min(minVal, root.val)
// 前往下一层
find(root.left, max1, min1)
find(root.right, max1, min1)
}
find(root, root.val, root.val)
return res
}
0x03 我的作品
欢迎体验我的作品之一:小五笔
五笔学习好帮手App Store
搜索即可~
边栏推荐
- Sobel filter
- Lost in the lock world of MySQL
- Feature scaling normalization
- Redis introduction complete tutorial: detailed explanation of ordered collection
- A complete tutorial for getting started with redis: redis usage scenarios
- Persistence mechanism of redis
- Attack and defense world misc advanced area Hong
- How can enterprises cross the digital divide? In cloud native 2.0
- sobel过滤器
- Wechat official account solves the cache problem of entering from the customized menu
猜你喜欢
VIM editor knowledge summary
Attack and defense world misc advanced area can_ has_ stdio?
Detailed explanation of heap sort code
EditPlus--用法--快捷键/配置/背景色/字体大小
【图论】拓扑排序
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Editplus-- usage -- shortcut key / configuration / background color / font size
Explanation of bitwise operators
Install the gold warehouse database of NPC
【机器学习】手写数字识别
随机推荐
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Attack and defense world misc master advanced zone 001 normal_ png
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
小程序vant tab组件解决文字过多显示不全的问题
Advanced area of attack and defense world misc 3-11
Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?
位运算符讲解
ECS settings SSH key login
微信小程序显示样式知识点总结
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
Analysis of environmental encryption technology
Detailed explanation of heap sort code
模拟摇杆控制舵机
Redis入门完整教程:事务与Lua
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
页面关闭前,如何发送一个可靠请求
集群的概述与定义,一看就会
10 schemes to ensure interface data security
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)