当前位置:网站首页>[binary tree] delete points to form a forest
[binary tree] delete points to form a forest
2022-07-07 12:47:00 【How cold it is】
0x00 subject
Give the root node of the binary tree root
Each node in the tree has a different value
If the node value is to_delete
It appears that
Let's take this node from the tree delete
Finally get a forest ( A collection of disjoint trees )
Return to every tree in the forest
You can organize the answers in any order
0x01 Ideas
A node needs Delete
when
It needs to be judged about
Whether the node is empty
If it is not empty add to
To the result array
And then return a Blank nodes
(nil) To the top
The upper layer can be directly replaced
therefore , So this is the way to think about it
Use In the following order
Traversal mode is ok
0x02 solution
Language :Swift
Tree node :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
}
}
solution :
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 }
// Delete the left and right subtrees respectively
let left = del(root.left, to_delete)
let right = del(root.right, to_delete)
// Update the left and right subtrees
root.left = left
root.right = right
// Nodes need to be deleted
if to_delete.contains(root.val) {
if left != nil {
res.append(left)
}
if right != nil {
res.append(right)
}
return nil
}
return root
}
// Whether the root node needs to be deleted ?
let r = del(root, to_delete)
if r != nil {
res.append(r)
}
return res
}
0x03 My little work
Welcome to experience one of my works : Small editor -XCompiler
Online editor , There are many languages App Store
Search ~
边栏推荐
- Common knowledge of one-dimensional array and two-dimensional array
- Utiliser la pile pour convertir le binaire en décimal
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- Airserver automatically receives multi screen projection or cross device projection
- layer弹出层的关闭问题
- JS to convert array to tree data
- 图像像素读写操作
- 解密GD32 MCU产品家族,开发板该怎么选?
- Configure an encrypted web server
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
猜你喜欢
2022聚合工艺考试题模拟考试题库及在线模拟考试
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
Minimalist movie website
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
SQL head injection -- injection principle and essence
Polymorphism, final, etc
MPLS experiment
Static vxlan configuration
随机推荐
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
MPLS experiment
Day-15 common APIs and exception mechanisms
有什么类方法或是函数可以查看某个项目的Laravel版本的?
基于NeRF的三维内容生成
[difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
【PyTorch实战】图像描述——让神经网络看图讲故事
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
【从 0 开始学微服务】【01】什么是微服务
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
ip2long与long2IP 分析
Using stack to convert binary to decimal
opencv的四个函数
Static comprehensive experiment
JS to convert array to tree data
Importance of database security
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)