当前位置:网站首页>【二叉树】删点成林
【二叉树】删点成林
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 搜索即可~
边栏推荐
- 爱可可AI前沿推介(7.7)
- Review and arrangement of HCIA
- Decrypt gd32 MCU product family, how to choose the development board?
- NGUI-UILabel
- Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
- Typescript interface inheritance
- Apache installation problem: configure: error: APR not found Please read the documentation
- Epp+dis learning path (1) -- Hello world!
- idea 2021中文乱码
猜你喜欢

Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
![[statistical learning methods] learning notes - improvement methods](/img/c5/515f171995da8e424de290228b54f8.png)
[statistical learning methods] learning notes - improvement methods

关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例

SQL head injection -- injection principle and essence

College entrance examination composition, high-frequency mention of science and Technology

leetcode刷题:二叉树27(删除二叉搜索树中的节点)

小红书微服务框架及治理等云原生业务架构演进案例

数据库系统原理与应用教程(011)—— 关系数据库

Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being

SQL Lab (46~53) (continuous update later) order by injection
随机推荐
Completion report of communication software development and Application
小红书微服务框架及治理等云原生业务架构演进案例
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
EPP+DIS学习之路(2)——Blink!闪烁!
Realize all, race, allsettled and any of the simple version of promise by yourself
OSPF exercise Report
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
[pytorch practice] image description -- let neural network read pictures and tell stories
When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
EPP+DIS学习之路(1)——Hello world!
<No. 8> 1816. Truncate sentences (simple)
Will the filing free server affect the ranking and weight of the website?
Minimalist movie website
【统计学习方法】学习笔记——支持向量机(上)
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
Problem: the string and characters are typed successively, and the results conflict
Learning and using vscode
gcc 编译报错
Cenos openssh upgrade to version 8.4