当前位置:网站首页>【二叉树】删点成林
【二叉树】删点成林
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 搜索即可~
边栏推荐
- Minimalist movie website
- What is a LAN domain name? How to parse?
- What is an esp/msr partition and how to create an esp/msr partition
- 【统计学习方法】学习笔记——支持向量机(上)
- Introduction and application of smoothstep in unity: optimization of dissolution effect
- VSCode的学习使用
- About sqli lab less-15 using or instead of and parsing
- 浅谈估值模型 (二): PE指标II——PE Band
- [Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
- 【深度学习】图像多标签分类任务,百度PaddleClas
猜你喜欢

Problem: the string and characters are typed successively, and the results conflict

【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器

Epp+dis learning road (2) -- blink! twinkle!

浅谈估值模型 (二): PE指标II——PE Band

idea 2021中文乱码

SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)

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

盘点JS判断空对象的几大方法

解密GD32 MCU产品家族,开发板该怎么选?

College entrance examination composition, high-frequency mention of science and Technology
随机推荐
【统计学习方法】学习笔记——提升方法
Introduction and application of smoothstep in unity: optimization of dissolution effect
Static comprehensive experiment
免备案服务器会影响网站排名和权重吗?
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
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
Error in compiling libssl
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
On valuation model (II): PE index II - PE band
idm服务器响应显示您没有权限下载解决教程
Completion report of communication software development and Application
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
Preorder, inorder and postorder traversal of binary tree
Zero shot, one shot and few shot
跨域问题解决方案
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
2022 8th "certification Cup" China University risk management and control ability challenge
Tutorial on principles and applications of database system (009) -- conceptual model and data model
Solutions to cross domain problems