当前位置:网站首页>【二叉树】根到叶路径上的不足节点
【二叉树】根到叶路径上的不足节点
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 搜索即可~
边栏推荐
- Little knowledge about C language (array and string)
- 张平安:加快云上数字创新,共建产业智慧生态
- Jarvis OJ 简单网管协议
- Games101 notes (I)
- 编译libssh2报错找不到openssl
- If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
- CMake教程Step3(添加库的使用要求)
- Games101 notes (II)
- 采用药丸屏的iPhone14或引发中国消费者的热烈抢购
- Jarvis OJ webshell analysis
猜你喜欢

Deep dive kotlin synergy (XXI): flow life cycle function

激动人心!2022开放原子全球开源峰会报名火热开启!

【性能测试】jmeter+Grafana+influxdb部署实战

Jarvis OJ Telnet Protocol
![[61dctf]fm](/img/22/3e4e3f1679a27d8b905684bb709905.png)
[61dctf]fm

Hiengine: comparable to the local cloud native memory database engine
![[729. My schedule I]](/img/e3/32914227d00cf7595ee850e60f2b72.png)
[729. My schedule I]

国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
![[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details

PHP talent recruitment system development source code recruitment website source code secondary development
随机推荐
ECU简介
thinkphp模板的使用
Summary of PHP pseudo protocol of cisp-pte
Jarvis OJ webshell analysis
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
Get ready for the pre-season card game MotoGP ignition champions!
How can C TCP set heartbeat packets to be elegant?
[Jianzhi offer] 63 Maximum profit of stock
[brush questions] effective Sudoku
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
dried food! Semi supervised pre training dialogue model space
一个满分的项目文档是如何书写的|得物技术
The third lesson of EasyX learning
浏览器渲染原理以及重排与重绘
Thoughtworks 全球CTO:按需求构建架构,过度工程只会“劳民伤财”
Iphone14 with pill screen may trigger a rush for Chinese consumers
What else do you not know about new map()
[Jianzhi offer] 66 Build product array
Judge whether a string is a full letter sentence
PHP人才招聘系统开发 源代码 招聘网站源码二次开发