当前位置:网站首页>[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 ~
边栏推荐
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- SQL Lab (46~53) (continuous update later) order by injection
- opencv的四个函数
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- [疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
- ps链接图层的使用方法和快捷键,ps图层链接怎么做的
- [statistical learning method] learning notes - support vector machine (I)
- 普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
- 利用棧來實現二進制轉化為十進制
- Using stack to convert binary to decimal
猜你喜欢
[statistical learning methods] learning notes - improvement methods
Charles: four ways to modify the input parameters or return results of the interface
图形对象的创建与赋值
Visual stdio 2017 about the environment configuration of opencv4.1
Attack and defense world - PWN learning notes
SQL head injection -- injection principle and essence
Minimalist movie website
浅谈估值模型 (二): PE指标II——PE Band
Configure an encrypted web server
Experiment with a web server that configures its own content
随机推荐
Experiment with a web server that configures its own content
编译 libssl 报错
Session
解密GD32 MCU产品家族,开发板该怎么选?
SQL head injection -- injection principle and essence
BGP third experiment report
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
【PyTorch实战】用RNN写诗
leetcode刷题:二叉树23(二叉搜索树中的众数)
[deep learning] image multi label classification task, Baidu paddleclas
Talk about four cluster schemes of redis cache, and their advantages and disadvantages
爱可可AI前沿推介(7.7)
On valuation model (II): PE index II - PE band
Four functions of opencv
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
JS to convert array to tree data
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
SSM框架搭建的步骤
Polymorphism, final, etc
[statistical learning methods] learning notes - Chapter 5: Decision Tree