当前位置:网站首页>LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
2022-08-11 03:58:00 【small double】
LeetCode814 二叉树剪枝
剪枝:Pruning, as the name suggests, is the pruning of a tree,In reality, the purpose of pruning a tree is to make it look better,The same is true for operations on binary trees in programs,called in the program“好看”That is, the time complexity of the algorithm can be reduced.Pruning is like a decision tree,It is useful in areas such as machine learning overfitting,But I don't understand the use of pruning in real scenarios,It's just a matter of understanding from the point of view of the question(害).
题目描述
Given a binary tree root noderoot,此外树的每个结点的值要么是 0,要么是 1.
Return to remove all not included1的子树的原二叉树
节点x的子树包含节点xitself and allx的后代
说明:
给定的二叉树最多有100个节点
每个节点的值只会为0或1
示例
输入:[1,null,0,0,1]
输出:[1,null,0,null,1]
图示:
说明:
Only the red one0满足“All subtree nodes are not included1”,另外一个0Its right subtree obviously has one1,所以不用删除,Note that the stipulations given in the title are:节点x的子树包含节点xitself and allx的后代
输入:[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]
图示:
题目解析
由上面的示例可以看出,The value of each removed node is 0,And its left and right subtrees are both “空”,即是“叶子节点”.拿示例2来看,If we delete it recursively,The recursive stack first operates on the last node pushed onto the stack,即The recursive way is bottom-up进行操作的.This is when the example is formally processed2the second node in 0(从左往右算)时,Two of its left and right subtrees0have all been deleted,So this node can also be deleted,Repeat this process to delete all nodes that meet the conditions.The code for the recursive implementation is concise,代码如下:
class Solution {
public TreeNode pruneTree(TreeNode root) {
if (root == null) {
return null;
}
root.left = pruneTree(root.left); // Get the new subtree after pruning the left subtree
root.right = pruneTree(root.right); //Get the new subtree after pruning the right subtree
if (root.left == null && root.right == null && root.val == 0) {
//The left and right subtrees are empty and have values0,要删除,返回null即可
return null;
}
return root;
}
}
程序运行结果:
注:虽然形式很简单,But the key is to understand the recursive operation process.
For more solutions, see the original question:814.二叉树剪枝
边栏推荐
- Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
- 【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口
- Is there any way for kingbaseES to not read the system view under sys_catalog by default?
- En-us is an invalid culture error solution when Docker links sqlserver
- Kubernetes集群搭建Zabbix监控平台
- EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
- 【FPGA】名词缩写
- 【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
- Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
- Get the length of the linked list
猜你喜欢
(转)JVM中那些区域会发生OOM?
[C Language] Getting Started
[FPGA] day19- binary to decimal (BCD code)
Kubernetes集群搭建Zabbix监控平台
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
What is machine learning?Explain machine learning concepts in detail
"3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing
机器学习是什么?详解机器学习概念
什么是机器强化学习?原理是什么?
随机推荐
Will oracle cardinality affect query speed?
机器学习中什么是集成学习?
[ADI low-power 2k code] Based on ADuCM4050, ADXL363, TMP75 acceleration, temperature detection and serial port printing, buzzer playing music (lone warrior)
App基本框架搭建丨日志管理 - KLog
Qnet弱网测试工具操作指南
Element's BFC attribute
console.log alternatives you didn't know about
[FPGA] Design Ideas - I2C Protocol
How to delete statements audit log?
Kubernetes集群搭建Zabbix监控平台
蹭个热度-请勿打开
【FPGA】day21- moving average filter
What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?
es-head插件插入查询以及条件查询(五)
Alibaba Cloud releases 3 high-performance computing solutions
元素的BFC属性
"3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing
MYSQLg高级------回表
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
Is there any way for kingbaseES to not read the system view under sys_catalog by default?