当前位置:网站首页>【二叉树】删点成林
【二叉树】删点成林
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 搜索即可~
边栏推荐
- What are the top-level domain names? How is it classified?
- ENSP MPLS layer 3 dedicated line
- Typescript interface inheritance
- gcc 编译报错
- On valuation model (II): PE index II - PE band
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
- 跨域问题解决方案
- The road to success in R & D efficiency of 1000 person Internet companies
- What is a LAN domain name? How to parse?
- 【统计学习方法】学习笔记——支持向量机(下)
猜你喜欢

About sqli lab less-15 using or instead of and parsing

The left-hand side of an assignment expression may not be an optional property access.ts(2779)

Zero shot, one shot and few shot

Decrypt gd32 MCU product family, how to choose the development board?

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

Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition

idea 2021中文乱码

SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)

The left-hand side of an assignment expression may not be an optional property access. ts(2779)

百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
随机推荐
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
【统计学习方法】学习笔记——提升方法
Problem: the string and characters are typed successively, and the results conflict
NGUI-UILabel
The IDM server response shows that you do not have permission to download the solution tutorial
跨域问题解决方案
idm服务器响应显示您没有权限下载解决教程
The road to success in R & D efficiency of 1000 person Internet companies
Object. Simple implementation of assign()
30. Feed shot named entity recognition with self describing networks reading notes
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
Tutorial on the principle and application of database system (011) -- relational database
College entrance examination composition, high-frequency mention of science and Technology
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
Up meta - Web3.0 world innovative meta universe financial agreement
利用棧來實現二進制轉化為十進制
【PyTorch实战】用RNN写诗
Vxlan 静态集中网关
Static routing assignment of network reachable and telent connections
【统计学习方法】学习笔记——第五章:决策树