当前位置:网站首页>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.二叉树剪枝
边栏推荐
- "110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series
- 一文读懂 高性能可预期数据中心网络
- 我的 archinstall 使用手册
- 华南师范宋宇老师课堂对话论文翻译
- 程序化交易的策略类型可以分为哪几种?
- The development of the massage chair control panel makes the massage chair simple and intelligent
- console.log alternatives you didn't know about
- The impact of programmatic trading and subjective trading on the profit curve!
- What are port 80 and port 443?What's the difference?
- Redis老了吗?Redis与Dragonfly性能比较
猜你喜欢

校园兼职平台项目反思

What is ensemble learning in machine learning?
![Binary tree related code questions [more complete] C language](/img/85/a109eed69cd54be3c8290e8dd67b7c.png)
Binary tree related code questions [more complete] C language

Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)

What is Machine Reinforcement Learning?What is the principle?

EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?

电力机柜数据监测RTU

Is Redis old?Performance comparison between Redis and Dragonfly

Day20 FPGA 】 【 - block the I2C read and write EEPROM

Detailed explanation of VIT source code
随机推荐
.NET Custom Middleware
Redis老了吗?Redis与Dragonfly性能比较
Is Redis old?Performance comparison between Redis and Dragonfly
Qnet Weak Network Test Tool Operation Guide
STC8H development (15): GPIO drive Ci24R1 wireless module
What is machine learning?Explain machine learning concepts in detail
Callable实现多线程
Pinduoduo store business license related issues
【FPGA】day21-移动平均滤波器
云平台下ESB产品开发步骤说明
蹭个热度-请勿打开
【C语言】入门
Element's BFC attribute
Enter the starting position, the ending position intercepts the linked list
“顶梁柱”滑坡、新增长极难担重任,阿里“蹲下”是为了跳更高?
(转)JVM中那些区域会发生OOM?
学编程的第十三天
A simple JVM tuning, learn to write it on your resume
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
js 将字符串作为js执行代码使用