当前位置:网站首页>[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 < limitsum Is on the path node Sum of all previous nodes
On the contrary
Every time we pass through a node , Can be updated limitlimit - 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 ~
边栏推荐
- 【剑指 Offer】66. 构建乘积数组
- NPM installation
- About JSON parsing function JSON in MySQL_ EXTRACT
- 33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
- How MySQL uses JSON_ Extract() takes JSON value
- Machine learning 02: model evaluation
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
- [wechat applet] read the life cycle and route jump of the applet
- Machine learning 01: Introduction
猜你喜欢

ECU introduction

How to write a full score project document | acquisition technology
Tips for extracting JSON fields from MySQL
Summary of optimization scheme for implementing delay queue based on redis

China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users

Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city

Deeply cultivate 5g, and smart core continues to promote 5g applications

Jarvis OJ Telnet Protocol

【Web攻防】WAF检测技术图谱

The second day of learning C language for Asian people
随机推荐
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
[Jianzhi offer] 63 Maximum profit of stock
Embedded-c Language-1
【性能测试】jmeter+Grafana+influxdb部署实战
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
Function sub file writing
The first lesson of EasyX learning
Use byte stream to read Chinese from file to console display
MySql 查询符合条件的最新数据行
[Jianzhi offer] 66 Build product array
干货!半监督预训练对话模型 SPACE
Application of threshold homomorphic encryption in privacy Computing: Interpretation
Is it safe for qiniu business school to open a stock account? Is it reliable?
CMake教程Step1(基本起点)
Learn about MySQL transaction isolation level
【剑指 Offer】63. 股票的最大利润
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Embedded-c Language-4
激动人心!2022开放原子全球开源峰会报名火热开启!
Wsl2.0 installation