当前位置:网站首页>【二叉树】删点成林
【二叉树】删点成林
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 搜索即可~
边栏推荐
- [pytorch practice] image description -- let neural network read pictures and tell stories
- What is an esp/msr partition and how to create an esp/msr partition
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
- 111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
- 解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- SQL Lab (41~45) (continuous update later)
- OSPF exercise Report
- Learning and using vscode
- 千人规模互联网公司研发效能成功之路
- gcc 编译报错
猜你喜欢

ES底层原理之倒排索引

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

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

wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6

In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)

Preorder, inorder and postorder traversal of binary tree

Completion report of communication software development and Application

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

Sonar:cognitive complexity

File upload vulnerability - upload labs (1~2)
随机推荐
跨域问题解决方案
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
(to be deleted later) yyds, paid academic resources, please keep a low profile!
What is an esp/msr partition and how to create an esp/msr partition
idm服务器响应显示您没有权限下载解决教程
Completion report of communication software development and Application
"Series after reading" my God! It's so simple to understand throttling and anti shake~
Learning and using vscode
ES底层原理之倒排索引
平安证券手机行开户安全吗?
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
gcc 编译报错
Attack and defense world ----- summary of web knowledge points
NGUI-UILabel
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
Up meta - Web3.0 world innovative meta universe financial agreement
Realize all, race, allsettled and any of the simple version of promise by yourself
Solutions to cross domain problems
Typescript interface inheritance
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)