当前位置:网站首页>【二叉树】根到叶路径上的不足节点
【二叉树】根到叶路径上的不足节点
2022-07-05 16:40:00 【豪冷啊】
0x00 题目
给定一棵二叉树的根 root
考虑它所有从根到任何叶 路径
假如通过节点 node 的每种可能的 “根-叶”
路径上值的总和全都小于给定的 limit
则该节点被称之为不足节点,需要被删除
请你删除所有不足节点,并返回生成的二叉树的根
0x01 思路
因为要计算从根到叶的总和
所以使用深度优先的遍历算法
节点 node 被删除的条件是sum + node.val < limitsum 为路径上 node 前面所有节点的和
反过来讲
每经过一个节点,可以更新 limitlimit - node.val
当 node.val < limit 时
就要被删除
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 sufficientSubset(_ root: TreeNode?, _ limit: Int) -> TreeNode? {
guard let root = root else { return nil }
let left = root.left
let right = root.right
// 到达叶子节点
if left == nil && right == nil {
return root.val >= limit ? root : nil
}
// 经过一个节点,减小
let res = limit - root.val
if left != nil {
root.left = sufficientSubset(left, res)
}
if right != nil {
root.right = sufficientSubset(right, res)
}
// 更新左右节点后,都为空
// 说明以当前节点为根,到叶子节点的路径总和 小于 limit
// 所以当前节点也要被删除
if root.left == nil && root.right == nil {
return nil
}
return root
}
0x03 我的作品
欢迎体验我的作品之一:小五笔 86 版
五笔学习好帮手App Store 搜索即可~
边栏推荐
- 【testlink】TestLink1.9.18常见问题解决方法
- 【性能测试】全链路压测
- 張平安:加快雲上數字創新,共建產業智慧生態
- [61dctf]fm
- Games101 notes (III)
- C how TCP restricts the access traffic of a single client
- Combined use of vant popup+ other components and pit avoidance Guide
- WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
- Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
- Deeply cultivate 5g, and smart core continues to promote 5g applications
猜你喜欢

【729. 我的日程安排錶 I】

Solution of vant tabbar blocking content

【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试

一个满分的项目文档是如何书写的|得物技术

机器学习编译第2讲:张量程序抽象

Embedded-c Language-2

Learnopongl notes (I)

Jarvis OJ Telnet Protocol

Embedded UC (UNIX System Advanced Programming) -1

调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
随机推荐
Judge whether a number is a prime number (prime number)
ECU introduction
齐宣王典故
C how TCP restricts the access traffic of a single client
張平安:加快雲上數字創新,共建產業智慧生態
Cs231n notes (bottom) - applicable to 0 Foundation
Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
easyNmon使用汇总
Practical example of propeller easydl: automatic scratch recognition of industrial parts
ternary operator
Wsl2.0 installation
【微信小程序】一文读懂小程序的生命周期和路由跳转
Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
C# TCP如何设置心跳数据包,才显得优雅呢?
[Web attack and Defense] WAF detection technology map
麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
Yarn common commands
Three traversal methods of binary tree
thinkphp模板的使用