当前位置:网站首页>[binary tree] the maximum difference between a node and its ancestor
[binary tree] the maximum difference between a node and its ancestor
2022-07-04 23:15:00 【How cold it is】
0x00 subject
Given the root node of a binary tree root
Find out what exists in Different
node A
and B
Maximum value between V
among V = |A.val - B.val|
And A
yes B
The ancestors of the
If A
Any of the Son
One of the nodes is B
perhaps A
Any of the Son
Node is B
Of The ancestors
So we think A
yes B
The ancestors of the
Corn seed :
We have a large number of differences between nodes and their ancestors , Some of them are as follows :|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Of all possible differences , Maximum 7
from |8 - 1| = 7
obtain
0x01 Ideas
Just look at one path :8-3-6-4
To calculate the difference 8-3,8-6,8-4
3-6,3-4
6-4
Then in these differences
obtain Maximum
One of the Difference value
To maximize the difference
Just find it in the path Maximum
and minimum value
therefore
Every time we pass through a node
Can to update
Maximum and minimum
And find out Difference value
0x02 solution
Language :Swift
Tree node :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
}
}
solution :
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 }
// Calculate the difference
var max1 = abs(maxVal - root.val)
var min1 = abs(minVal - root.val)
// Get the maximum difference
let big = max(max1, min1)
// Update results
res = max(res, big)
// Update maximum , minimum value
max1 = max(maxVal, root.val)
min1 = min(minVal, root.val)
// Go to the next floor
find(root.left, max1, min1)
find(root.right, max1, min1)
}
find(root, root.val, root.val)
return res
}
0x03 My work
Welcome to experience one of my works : Small five strokes
Wubi learning is a good helper App Store
Search ~
边栏推荐
- Servlet+jdbc+mysql simple web exercise
- QT drawing network topology diagram (connecting database, recursive function, infinite drawing, dragging nodes)
- Redis入门完整教程:慢查询分析
- Docker镜像的缓存特性和Dockerfile
- A complete tutorial for getting started with redis: hyperloglog
- Google collab trample pit
- Advantages of Alibaba cloud international CDN
- Mysql database backup and recovery -- mysqldump command
- Observable time series data downsampling practice in Prometheus
- debug和release的区别
猜你喜欢
Redis: redis configuration file related configuration and redis persistence
MariaDB's Galera cluster application scenario -- multi master and multi active databases
壁仞科技研究院前沿技术文章精选
智力考验看成语猜古诗句微信小程序源码
colResizable. JS auto adjust table width plug-in
【js】-【排序-相关】-笔记
The difference between cout/cerr/clog
Redis入门完整教程:初识Redis
Observable time series data downsampling practice in Prometheus
[roommate learned to use Bi report data processing in the time of King glory in one game]
随机推荐
高通WLAN框架学习(30)-- 支持双STA的组件
ETCD数据库源码分析——处理Entry记录简要流程
MariaDB's Galera cluster application scenario -- multi master and multi active databases
Redis getting started complete tutorial: hash description
Redis getting started complete tutorial: publish and subscribe
[crawler] XPath for data extraction
PICT 生成正交测试用例教程
微信公众号解决从自定义菜单进入的缓存问题
colResizable. JS auto adjust table width plug-in
ffmpeg快速剪辑
Redis入门完整教程:Bitmaps
The difference between debug and release
Redis入门完整教程:集合详解
为什么信息图会帮助你的SEO
Redis introduction complete tutorial: Collection details
MariaDB的Galera集群-双主双活安装设置
【js】-【动态规划】-笔记
The difference between cout/cerr/clog
List related knowledge points to be sorted out
EditPlus--用法--快捷键/配置/背景色/字体大小