当前位置:网站首页>【二叉树】删点成林
【二叉树】删点成林
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 搜索即可~
边栏推荐
- Will the filing free server affect the ranking and weight of the website?
- College entrance examination composition, high-frequency mention of science and Technology
- <No. 8> 1816. Truncate sentences (simple)
- 密码学系列之:在线证书状态协议OCSP详解
- Cenos openssh upgrade to version 8.4
- 消息队列消息丢失和消息重复发送的处理策略
- The IDM server response shows that you do not have permission to download the solution tutorial
- SQL blind injection (WEB penetration)
- Sonar:cognitive complexity
- SQL lab 1~10 summary (subsequent continuous update)
猜你喜欢

Simple network configuration for equipment management

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

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

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

Preorder, inorder and postorder traversal of binary tree

30. Feed shot named entity recognition with self describing networks reading notes

Hi3516全系统类型烧录教程

ES底层原理之倒排索引

对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景

ps链接图层的使用方法和快捷键,ps图层链接怎么做的
随机推荐
Is it safe to open an account in Ping An Securities mobile bank?
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
The IDM server response shows that you do not have permission to download the solution tutorial
Utiliser la pile pour convertir le binaire en décimal
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
Inverted index of ES underlying principle
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
Zero shot, one shot and few shot
开发一个小程序商城需要多少钱?
VSCode的学习使用
平安证券手机行开户安全吗?
Solutions to cross domain problems
Airserver automatically receives multi screen projection or cross device projection
2022 8th "certification Cup" China University risk management and control ability challenge
密码学系列之:在线证书状态协议OCSP详解
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
SQL head injection -- injection principle and essence
leetcode刷题:二叉树27(删除二叉搜索树中的节点)