当前位置:网站首页>[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 ~
边栏推荐
- 【剑指Offer】6-10题
- Editplus-- usage -- shortcut key / configuration / background color / font size
- Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
- Redis入门完整教程:HyperLogLog
- 可观测|时序数据降采样在Prometheus实践复盘
- 字体设计符号组合多功能微信小程序源码
- Photoshop批量给不同的图片添加不同的编号
- A complete tutorial for getting started with redis: transactions and Lua
- cout/cerr/clog的区别
- ECS settings SSH key login
猜你喜欢
Redis入门完整教程:初识Redis
Redis introduction complete tutorial: client communication protocol
Excel shortcut keys - always add
CTF competition problem solution STM32 reverse introduction
Docker镜像的缓存特性和Dockerfile
MariaDB的Galera集群应用场景--数据库多主多活
【剑指Offer】6-10题
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
Analysis of the self increasing and self decreasing of C language function parameters
Redis: redis transactions
随机推荐
CTF竞赛题解之stm32逆向入门
Notepad++ -- editing skills
Sword finger offer 68 - ii The nearest common ancestor of binary tree
The initial trial is the cross device model upgrade version of Ruijie switch (taking rg-s2952g-e as an example)
Redis入門完整教程:Pipeline
图片懒加载的原理
PS style JS webpage graffiti board plug-in
Google collab trample pit
一次edu证书站的挖掘
[try to hack] wide byte injection
MariaDB's Galera cluster application scenario -- multi master and multi active databases
The difference between debug and release
Excel shortcut keys - always add
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
Redis getting started complete tutorial: Key Management
Application of machine learning in housing price prediction
Basic knowledge of database
VIM editor knowledge summary
MariaDB的Galera集群-双主双活安装设置