当前位置:网站首页>【二叉树】二叉树剪枝
【二叉树】二叉树剪枝
2022-06-10 22:30:00 【豪冷啊】
0x00 题目
给你二叉树的根结点 root
此外树的每个结点的值要么是 0,要么是 1
返回移除了所有不包含 1 的子树的原二叉树
节点 node 的子树为 node 本身
加上所有 node 的后代
0x01 思路
叶子节点值为 0 时,去掉
某个节点的如果要去掉
则左子树的值全为 0
右子树的值是全为 0
再加上节点本身的值也是 0
反过来讲,也就是
以某个节点为根的子树
只要存在值为 1 的节点
则这棵子树不用删除
0x02 解法
语言:Swift
树节点: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
}
}
解法:
func pruneTree(_ root: TreeNode?) -> TreeNode? {
func trim(_ root: TreeNode?) -> Bool {
guard let root = root else {
return false
}
let left = trim(root.left)
let right = trim(root.right)
// 因为要从叶子节点开始剪,所以使用后序遍历方式
// 左子树中全是 0
if left == false {
root.left = nil
}
// 右子树中全是 0
if right == false {
root.right = nil
}
// 左子树,右子树,节点,整个子树是否全为 0
return left || right || (root.val == 1)
}
// 整棵树都为 0,则返回 nil
let b = trim(root)
return b ? root : nil
}
0x03 我的作品
欢迎体验我的作品之一:小五笔86版
五笔学习好帮手 App Store 搜索即可~
边栏推荐
- Halcon combined with C # to detect surface defects -- affine transformation (II)
- 34. find the first and last positions of elements in the sorted array - binary search, double pointer
- Is it too late to teach yourself programming at 28? Is it reliable?
- PwnTheBox,Web:hello
- 中银证券股票开户安全吗?是正规的吗?
- Data and information resource sharing platform (V)
- 嵌入式软件开发中的2种调试技术
- Ribbon load balancing policy
- 功能测试之设计语言测试:功能测试包含哪些测试?分别有什么作用
- Font spider Teaching -- ttf/otf font file compression
猜你喜欢

What Fiddler does for testing

苹果CMS采集站源码-搭建教程-附带源码-全新源码-开发文档

Project training 10 - backup of specific databases

SQL查询优化原理实例分析
![The shell script of pagoda plan task regularly deletes all files under a directory [record]](/img/dc/cfd449bf14c4545cd2e52bda4ab31e.png)
The shell script of pagoda plan task regularly deletes all files under a directory [record]

Yuntu says that every successful business system cannot be separated from apig

Wireshark抓取rtp负载ts流介绍(UDP组播)

Unity 脚本无法显示C#源码的中文注释 或者VS创建的脚本没有C#源码的注释

LeetCode+ 21 - 25

PHP implementation of iframe cross site text replacement / iframe website text replacement
随机推荐
R language to draw two-dimensional normal distribution density surface;
[Video] kmeans mean clustering and hierarchical clustering: R language analysis life happiness index visualization | data sharing
The shell script of pagoda plan task regularly deletes all files under a directory [record]
What are the common methods of object
1.打开R程序,使用 apply 函数计算1到12按先后次序每隔3个数一组的和。即,计算1+4+7+10=?2+5+8+11=?,3+6+9+12=?
Relevant knowledge of flowable BPMN
Project training 12 - parsing SQL statements for creating tables
Is it safe for CICC Fortune Securities to open an account? Is it reliable?
The time (in minutes) required for a group of workers to cooperate to complete the assembly process of a part are as follows:
PwnTheBox,Pwn:tutorial1
Interview questions - written examination
Unity 脚本无法显示C#源码的中文注释 或者VS创建的脚本没有C#源码的注释
Executor - Shutdown、ShutdownNow、awaitTermination 详解与实战
mysql 表机制
Is it safe to open an account for tongdaxin stock? How to open an account?
PwnTheBox,Pwn:tutorial1
MySQL learning child query
LeetCode 501 :二叉搜索樹中的眾數
上海证券开户是安全的吗?
启牛的证券是哪个证券公司的呢?安全吗?