当前位置:网站首页>【二叉树】删点成林
【二叉树】删点成林
2022-07-07 10:30:00 【豪冷啊】
0x00 题目
给出二叉树的根节点 root
树上每个节点都有一个不同的值
如果节点值在 to_delete 中出现
我们就把该节点从树上删去
最后得到一个森林(一些不相交的树构成的集合)
返回森林中的每棵树
你可以按任意顺序组织答案
0x01 思路
某个节点需要删除时
需要判断它的左右节点是否为空
不为空则添加到结果数组中
然后返回一个空节点(nil) 给上层
上层直接替换即可
所以,按照这个思路
使用后序遍历方式即可
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 delNodes(_ root: TreeNode?, _ to_delete: [Int]) -> [TreeNode?] {
var res: [TreeNode?] = []
func del(_ root: TreeNode?, _ to_delete: [Int]) -> TreeNode? {
guard let root = root else { return nil }
// 分别去左右子树进行删除操作
let left = del(root.left, to_delete)
let right = del(root.right, to_delete)
// 更新左右子树
root.left = left
root.right = right
// 节点需要删除
if to_delete.contains(root.val) {
if left != nil {
res.append(left)
}
if right != nil {
res.append(right)
}
return nil
}
return root
}
// 根节点是否需要删除?
let r = del(root, to_delete)
if r != nil {
res.append(r)
}
return res
}
0x03 我的小作品
欢迎体验我的作品之一:小编辑器-XCompiler
在线编辑器,包含多种语言App Store 搜索即可~
边栏推荐
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
- Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
- (待会删)yyds,付费搞来的学术资源,请低调使用!
- 编译 libssl 报错
- About web content security policy directive some test cases specified through meta elements
- PowerShell cs-utf-16le code goes online
- Multi row and multi column flex layout
- Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
猜你喜欢

数据库系统原理与应用教程(009)—— 概念模型与数据模型

【统计学习方法】学习笔记——支持向量机(下)

PowerShell cs-utf-16le code goes online

Epp+dis learning path (1) -- Hello world!

Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具

Vxlan 静态集中网关

Inverted index of ES underlying principle

Configure an encrypted web server

(to be deleted later) yyds, paid academic resources, please keep a low profile!

The left-hand side of an assignment expression may not be an optional property access. ts(2779)
随机推荐
sql-lab (54-65)
[statistical learning methods] learning notes - improvement methods
数据库系统原理与应用教程(011)—— 关系数据库
Realize all, race, allsettled and any of the simple version of promise by yourself
@Bean与@Component用在同一个类上,会怎么样?
JS to convert array to tree data
IPv6 experiment
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
The IDM server response shows that you do not have permission to download the solution tutorial
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
开发一个小程序商城需要多少钱?
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
EPP+DIS学习之路(2)——Blink!闪烁!
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
静态Vxlan 配置
[pytorch practice] image description -- let neural network read pictures and tell stories
About sqli lab less-15 using or instead of and parsing
跨域问题解决方案