当前位置:网站首页>[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 ~
边栏推荐
- What if does not match your user account appears when submitting the code?
- How to use PS link layer and shortcut keys, and how to do PS layer link
- Utiliser la pile pour convertir le binaire en décimal
- Several ways to clear floating
- 【深度学习】图像多标签分类任务,百度PaddleClas
- SQL blind injection (WEB penetration)
- [爬虫]使用selenium时,躲避脚本检测
- 利用栈来实现二进制转化为十进制
- Simple implementation of call, bind and apply
- [statistical learning method] learning notes - support vector machine (I)
猜你喜欢
2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
静态Vxlan 配置
【PyTorch实战】用RNN写诗
leetcode刷题:二叉树23(二叉搜索树中的众数)
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
AirServer自动接收多画面投屏或者跨设备投屏
[statistical learning method] learning notes - support vector machine (Part 2)
RHSA first day operation
基于NeRF的三维内容生成
随机推荐
Charles: four ways to modify the input parameters or return results of the interface
GCC compilation error
MPLS experiment
Learning and using vscode
leetcode刷题:二叉树23(二叉搜索树中的众数)
Static routing assignment of network reachable and telent connections
解密GD32 MCU产品家族,开发板该怎么选?
Day-15 common APIs and exception mechanisms
SQL Lab (41~45) (continuous update later)
[deep learning] image multi label classification task, Baidu paddleclas
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
Customize the web service configuration file
ICLR 2022 | pre training language model based on anti self attention mechanism
[pytorch practice] write poetry with RNN
AirServer自动接收多画面投屏或者跨设备投屏
The IDM server response shows that you do not have permission to download the solution tutorial
Polymorphism, final, etc
Solutions to cross domain problems
About sqli lab less-15 using or instead of and parsing
【统计学习方法】学习笔记——支持向量机(下)