当前位置:网站首页>[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 ~
边栏推荐
- Static comprehensive experiment
- 聊聊Redis缓存4种集群方案、及优缺点对比
- 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)
- In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
- visual stdio 2017关于opencv4.1的环境配置
- SSM框架搭建的步骤
- [crawler] avoid script detection when using selenium
- Day-18 hash table, generic
- Multi row and multi column flex layout
- Static vxlan configuration
猜你喜欢
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
Zhimei creative website exercise
Preorder, inorder and postorder traversal of binary tree
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
Day-14 common APIs
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
SQL Lab (46~53) (continuous update later) order by injection
[statistical learning method] learning notes - support vector machine (I)
Image pixel read / write operation
如何将 @Transactional 事务注解运用到炉火纯青?
随机推荐
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
[learn micro services from 0] [02] move from single application to service
IPv6 experiment
Talk about four cluster schemes of redis cache, and their advantages and disadvantages
[statistical learning methods] learning notes - Chapter 4: naive Bayesian method
"Series after reading" my God! It's so simple to understand throttling and anti shake~
Vxlan static centralized gateway
leetcode刷题:二叉树21(验证二叉搜索树)
MPLS experiment
ICLR 2022 | pre training language model based on anti self attention mechanism
opencv的四个函数
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
[crawler] avoid script detection when using selenium
Day-19 IO stream
Attack and defense world ----- summary of web knowledge points
Master公式。(用于计算递归的时间复杂度。)
2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
聊聊Redis缓存4种集群方案、及优缺点对比
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘