当前位置:网站首页>[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 ~
边栏推荐
- 密码学系列之:在线证书状态协议OCSP详解
- Learning and using vscode
- SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
- 【统计学习方法】学习笔记——支持向量机(上)
- Importance of database security
- OSPF exercise Report
- [learn microservice from 0] [01] what is microservice
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
- 【从 0 开始学微服务】【01】什么是微服务
猜你喜欢

Charles: four ways to modify the input parameters or return results of the interface

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

【统计学习方法】学习笔记——支持向量机(下)

Attack and defense world - PWN learning notes

Zhimei creative website exercise

The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful

The IDM server response shows that you do not have permission to download the solution tutorial

Several ways to clear floating

About IPSec

Sorting, dichotomy
随机推荐
解密GD32 MCU产品家族,开发板该怎么选?
AirServer自动接收多画面投屏或者跨设备投屏
Realize all, race, allsettled and any of the simple version of promise by yourself
有什么类方法或是函数可以查看某个项目的Laravel版本的?
Solutions to cross domain problems
Image pixel read / write operation
[crawler] avoid script detection when using selenium
Utiliser la pile pour convertir le binaire en décimal
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
密码学系列之:在线证书状态协议OCSP详解
[statistical learning method] learning notes - support vector machine (I)
Vxlan 静态集中网关
Connect to blog method, overload, recursion
[learn micro services from 0] [02] move from single application to service
[learn wechat from 0] [00] Course Overview
如何将 @Transactional 事务注解运用到炉火纯青?
File upload vulnerability - upload labs (1~2)
BGP actual network configuration
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
Vxlan static centralized gateway