当前位置:网站首页>【二叉树】节点与其祖先之间的最大差值
【二叉树】节点与其祖先之间的最大差值
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-43-6,3-46-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 搜索即可~
边栏推荐
- 串口数据帧
- Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
- 图片懒加载的原理
- Gnawing down the big bone - sorting (II)
- 微信公众号解决从自定义菜单进入的缓存问题
- Sword finger offer 68 - I. nearest common ancestor of binary search tree
- Redis入门完整教程:列表讲解
- [graph theory] topological sorting
- 堆排序代码详解
- Common methods in string class
猜你喜欢

攻防世界 MISC 进阶区 Ditf
![[OpenGL] note 29 anti aliasing (MSAA)](/img/66/61f29e1c41d3099d55e2ead0a3b01e.png)
[OpenGL] note 29 anti aliasing (MSAA)

NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse

攻防世界 MISC 进阶区 can_has_stdio?

Attack and defense world misc advanced grace-50

Breakpoint debugging under vs2019 c release

A complete tutorial for getting started with redis: getting to know redis for the first time

位运算符讲解

The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
![[Jianzhi offer] 6-10 questions](/img/73/5974068008bcdc9a70b3f5f57f1eb0.png)
[Jianzhi offer] 6-10 questions
随机推荐
Redis introduction complete tutorial: Collection details
Sobel filter
Install the gold warehouse database of NPC
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
【机器学习】手写数字识别
Editplus-- usage -- shortcut key / configuration / background color / font size
Redis getting started complete tutorial: Key Management
Redis getting started complete tutorial: publish and subscribe
Principle of lazy loading of pictures
攻防世界 MISC 进阶区 hit-the-core
Redis入门完整教程:发布订阅
Persistence mechanism of redis
Analysis of the self increasing and self decreasing of C language function parameters
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
页面关闭前,如何发送一个可靠请求
【剑指offer】1-5题
EditPlus--用法--快捷键/配置/背景色/字体大小
浅聊一下中间件