当前位置:网站首页>LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
2022-08-11 03:42:00 【小机double】
LeetCode814 二叉树剪枝
剪枝:剪枝顾名思义就是对一棵树进行修修剪剪,现实中我们对树进行修剪的目的是为了让其更好看,而在程序中对二叉树进行操作也是如此的,在程序中所谓的“好看”即是可以减少算法的时间复杂度。剪枝好像在决策树,机器学习过度拟合等领域都有用到,但是我并不了解剪枝在真正场景中的使用,仅仅只是在做题的角度上有所了解(害)。
题目描述
给定二叉树根节点root,此外树的每个结点的值要么是 0,要么是 1.
返回移除所有不包含1的子树的原二叉树
节点x的子树包含节点x本身和所有x的后代
说明:
给定的二叉树最多有100个节点
每个节点的值只会为0或1
示例
输入:[1,null,0,0,1]
输出:[1,null,0,null,1]
图示:

说明:
只有红色那个0满足“所有子树节点不包含1”,另外一个0其右子树明显有个1,所以不用删除,要注意的是题目给出的规定是:节点x的子树包含节点x本身和所有x的后代
输入:[1,0,1,0,0,0,1]
输出:[1,null,1,null,1]

输入:[1,1,0,1,1,0,1,0]
输出:[1,1,0,1,1,null,1]
图示:

题目解析
由上面的示例可以看出,每个被删除的节点的值为0,并且其左右子树都为“空”,即是“叶子节点”。拿示例2来看,如果我们采用递归的方式进行删除,递归栈首先操作最后一个入栈的节点,即递归方式是自底向上进行操作的。这样当正式处理示例2中的第二节点0(从左往右算)时,其左右子树的两个0都已经被删除掉了,所以这个节点同样的也可以删除,重复这样的过程即可删除所有满足条件的节点。递归实现的代码很简洁,代码如下:
class Solution {
public TreeNode pruneTree(TreeNode root) {
if (root == null) {
return null;
}
root.left = pruneTree(root.left); // 获取对左子树剪枝后的新子树
root.right = pruneTree(root.right); //获取对右子树剪枝后的新子树
if (root.left == null && root.right == null && root.val == 0) {
//左右子树为空且值为0,要删除,返回null即可
return null;
}
return root;
}
}
程序运行结果:
注:虽然形式很简单,但是关键还是在于理解递归的运算过程。
更多题解请看原题:814.二叉树剪枝
边栏推荐
- A brief analysis of whether programmatic futures trading or manual order is better?
- The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
- JS-DOM element object
- 阿里低代码框架 lowcode-engine 之自定义物料篇
- 【FPGA】day19-二进制转换为十进制(BCD码)
- C语言之自定义类型------结构体
- 我的 archinstall 使用手册
- 你不知道的 console.log 替代品
- 【FPGA】day21- moving average filter
- Basic understanding of MongoDB (2)
猜你喜欢

浮点数在内存中的存储方式

AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis

STC8H development (15): GPIO drive Ci24R1 wireless module

Multi-serial port RS485 industrial gateway BL110

索引的创建、查看、删除

EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?

C语言之自定义类型------结构体

多商户商城系统功能拆解26讲-平台端分销设置

JS-DOM element object

A simple JVM tuning, learn to write it on your resume
随机推荐
LeetCode Hot Questions (12. The Best Time to Buy and Sell Stocks)
How to rebuild after pathman_config and pathman_config_params are deleted?
What are port 80 and port 443?What's the difference?
[BX] and loop
【FPGA】day18-ds18b20实现温度采集
浮点数在内存中的存储方式
How to delete statements audit log?
oracle的基数会影响到查询速度吗?
VIT 源码详解
watch监听
UNI-APP_iphone苹果手机底部安全区域
EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
The development of the massage chair control panel makes the massage chair simple and intelligent
[DB operation management/development solution] Shanghai Daoning provides you with an integrated development tool to improve the convenience of work - Orange
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
STC8H开发(十五): GPIO驱动Ci24R1无线模块
常用认证机制
AI + medical: for medical image recognition using neural network analysis
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
Binary tree related code questions [more complete] C language