当前位置:网站首页>[binary tree] insufficient nodes on the root to leaf path
[binary tree] insufficient nodes on the root to leaf path
2022-07-05 17:18:00 【How cold it is】
0x00 subject
Given the root of a binary tree root
Consider it all from root
To any leaf
route
If through the node node
Every possible “ root - leaf ”
Value on the path The sum of the
All Small
Depending on the given limit
Then this node is called Insufficient nodes
, Need to be Delete
Please delete all insufficient nodes , And return the root of the generated binary tree
0x01 Ideas
Because to calculate From root to leaf
The sum of
So use depth
Priority traversal algorithm
node node
The condition to be deleted is sum + node.val < limit
sum
Is on the path node
Sum of all previous nodes
On the contrary
Every time we pass through a node , Can be updated limit
limit - node.val
When node.val < limit
when
It's about to be deleted
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 sufficientSubset(_ root: TreeNode?, _ limit: Int) -> TreeNode? {
guard let root = root else { return nil }
let left = root.left
let right = root.right
// To the leaf node
if left == nil && right == nil {
return root.val >= limit ? root : nil
}
// Pass through a node , Reduce
let res = limit - root.val
if left != nil {
root.left = sufficientSubset(left, res)
}
if right != nil {
root.right = sufficientSubset(right, res)
}
// After updating the left and right nodes , All is empty
// Description: take the current node as the root , Sum of paths to leaf nodes Less than limit
// So the current node will also be deleted
if root.left == nil && root.right == nil {
return nil
}
return root
}
0x03 My work
Welcome to experience one of my works : Small five strokes 86 edition
Wubi learning is a good helper App Store
Search ~
边栏推荐
- Deeply cultivate 5g, and smart core continues to promote 5g applications
- 外盘期货平台如何辨别正规安全?
- 编译libssh2报错找不到openssl
- 33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
- Tips for extracting JSON fields from MySQL
- [7.7 live broadcast preview] the lecturer of "typical architecture of SaaS cloud native applications" teaches you to easily build cloud native SaaS applications. Once the problem is solved, Huawei's s
- Judge whether a string is a full letter sentence
- Embedded -arm (bare board development) -2
- Learn about MySQL transaction isolation level
- The third lesson of EasyX learning
猜你喜欢
The second day of learning C language for Asian people
Etcd build a highly available etcd cluster
Embedded -arm (bare board development) -2
阈值同态加密在隐私计算中的应用:解读
【机器人坐标系第一讲】
URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
Deeply cultivate 5g, and smart core continues to promote 5g applications
Summary of optimization scheme for implementing delay queue based on redis
CMake教程Step4(安装和测试)
What are the precautions for MySQL group by
随机推荐
flask解决CORS ERR 问题
Embedded-c Language-1
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
Jarvis OJ Telnet Protocol
MySql 查询符合条件的最新数据行
【性能测试】全链路压测
Embedded UC (UNIX System Advanced Programming) -1
China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users
机器学习02:模型评估
Judge whether a number is a prime number (prime number)
Deeply cultivate 5g, and smart core continues to promote 5g applications
Application of threshold homomorphic encryption in privacy Computing: Interpretation
Embedded -arm (bare board development) -2
7.Scala类
Use of ThinkPHP template
[first lecture on robot coordinate system]
[Jianzhi offer] 66 Build product array
composer安装报错:No composer.lock file present.
深入理解Redis内存淘汰策略
npm安装