当前位置:网站首页>【二叉树】根到叶路径上的不足节点
【二叉树】根到叶路径上的不足节点
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 搜索即可~
边栏推荐
- Browser rendering principle and rearrangement and redrawing
- The first lesson of EasyX learning
- CMake教程Step3(添加库的使用要求)
- Iphone14 with pill screen may trigger a rush for Chinese consumers
- 什么是ROM
- Games101 notes (II)
- [Jianzhi offer] 62 The last remaining number in the circle
- 【Web攻防】WAF检测技术图谱
- SQL injection of cisp-pte (Application of secondary injection)
- Keras crash Guide
猜你喜欢

SQL injection of cisp-pte (Application of secondary injection)

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

Games101 notes (III)

采用药丸屏的iPhone14或引发中国消费者的热烈抢购

高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积

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

Games101 notes (II)

兰空图床苹果快捷指令

Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.

Application of threshold homomorphic encryption in privacy Computing: Interpretation
随机推荐
tf. sequence_ Mask function explanation case
Yarn common commands
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
thinkphp3.2.3
【微信小程序】一文读懂小程序的生命周期和路由跳转
关于new Map( )还有哪些是你不知道的
How can C TCP set heartbeat packets to be elegant?
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
PHP talent recruitment system development source code recruitment website source code secondary development
Summary of PHP pseudo protocol of cisp-pte
張平安:加快雲上數字創新,共建產業智慧生態
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
项目引入jar从私服Nexus 拉去遇到的一个问题
飞桨EasyDL实操范例:工业零件划痕自动识别
Learnopongl notes (II) - Lighting
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
Wsl2.0 installation
URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
7.Scala类