当前位置:网站首页>【二叉树】删点成林
【二叉树】删点成林
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
搜索即可~
边栏推荐
- JS to convert array to tree data
- 对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
- GCC compilation error
- What are the top-level domain names? How is it classified?
- H3C HCl MPLS layer 2 dedicated line experiment
- Upgrade from a tool to a solution, and the new site with praise points to new value
- Introduction and application of smoothstep in unity: optimization of dissolution effect
- Niuke website
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- Customize the web service configuration file
猜你喜欢
Zhimei creative website exercise
盘点JS判断空对象的几大方法
【深度学习】图像多标签分类任务,百度PaddleClas
Zero shot, one shot and few shot
The IDM server response shows that you do not have permission to download the solution tutorial
浅谈估值模型 (二): PE指标II——PE Band
Minimalist movie website
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
idea 2021中文乱码
Static comprehensive experiment
随机推荐
开发一个小程序商城需要多少钱?
Configure an encrypted web server
What are the top-level domain names? How is it classified?
Problem: the string and characters are typed successively, and the results conflict
【PyTorch实战】图像描述——让神经网络看图讲故事
2022 8th "certification Cup" China University risk management and control ability challenge
Utiliser la pile pour convertir le binaire en décimal
EPP+DIS学习之路(2)——Blink!闪烁!
NGUI-UILabel
Epp+dis learning road (2) -- blink! twinkle!
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
Several methods of checking JS to judge empty objects
The road to success in R & D efficiency of 1000 person Internet companies
What is a LAN domain name? How to parse?
Zhimei creative website exercise
Zero shot, one shot and few shot
RHSA first day operation
SQL head injection -- injection principle and essence
Routing strategy of multi-point republication [Huawei]