当前位置:网站首页>【二叉树】删点成林
【二叉树】删点成林
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
搜索即可~
边栏推荐
- Vxlan 静态集中网关
- 【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
- [statistical learning methods] learning notes - improvement methods
- Configure an encrypted web server
- How to use PS link layer and shortcut keys, and how to do PS layer link
- 【统计学习方法】学习笔记——支持向量机(下)
- [pytorch practice] image description -- let neural network read pictures and tell stories
- <No. 8> 1816. Truncate sentences (simple)
- 2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
- EPP+DIS学习之路(1)——Hello world!
猜你喜欢
[deep learning] image multi label classification task, Baidu paddleclas
2022 8th "certification Cup" China University risk management and control ability challenge
Configure an encrypted web server
【深度学习】图像多标签分类任务,百度PaddleClas
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
ES底层原理之倒排索引
Zhimei creative website exercise
"Series after reading" my God! It's so simple to understand throttling and anti shake~
【统计学习方法】学习笔记——提升方法
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
随机推荐
About web content security policy directive some test cases specified through meta elements
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
idea 2021中文乱码
免备案服务器会影响网站排名和权重吗?
MPLS experiment
利用栈来实现二进制转化为十进制
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
NGUI-UILabel
Is it safe to open an account in Ping An Securities mobile bank?
Minimalist movie website
Utiliser la pile pour convertir le binaire en décimal
Tutorial on the principle and application of database system (011) -- relational database
BGP third experiment report
Decrypt gd32 MCU product family, how to choose the development board?
【PyTorch实战】用RNN写诗
SQL Lab (41~45) (continuous update later)
Problem: the string and characters are typed successively, and the results conflict
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
Multi row and multi column flex layout