当前位置:网站首页>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.二叉树剪枝
边栏推荐
- Basic understanding of MongoDB (2)
- set_new_handler(0)是什么意思?有什么用?
- The custom of the C language types -- -- -- -- -- - structure
- How to learn machine learning?machine learning process
- “顶梁柱”滑坡、新增长极难担重任,阿里“蹲下”是为了跳更高?
- App Basic Framework Construction丨Log Management - KLog
- 【FPGA】day19-二进制转换为十进制(BCD码)
- Getting Started with Raspberry Pi (5) System Backup
- 常见布局效果实现方案
- Description of ESB product development steps under cloud platform
猜你喜欢
"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series
How to learn machine learning?machine learning process
"98 BST and Its Verification" of the 13th day of leetcode brushing series of binary tree series
【FPGA】SDRAM
I didn't expect MySQL to ask these...
Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
Interchangeable Measurement Techniques - Geometric Errors
Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method
【愚公系列】2022年08月 Go教学课程 036-类型断言
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
随机推荐
What problems should we pay attention to when building a programmatic trading system?
Use jackson to parse json data in detail
Pinduoduo store business license related issues
浅析一下期货程序化交易好还是手工单好?
(转)JVM中那些区域会发生OOM?
App Basic Framework Construction丨Log Management - KLog
移动端地图开发选择哪家?
什么是机器强化学习?原理是什么?
"98 BST and Its Verification" of the 13th day of leetcode brushing series of binary tree series
Leetcode 450. 删除二叉搜索树中的节点
【FPGA】day20-I2C读写EEPROM
LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
UNI-APP_iphone bottom safe area
[FPGA] day19- binary to decimal (BCD code)
Callable实现多线程
Which one to choose for mobile map development?
.NET自定义中间件
EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
程序化交易改变了什么?
uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)