当前位置:网站首页>[daily training] 814. Binary tree pruning
[daily training] 814. Binary tree pruning
2022-07-23 13:18:00 【Puppet__】
subject
Give you the root node of the binary tree root , In addition, the value of each node of the tree is either 0 , Or 1 .
Return to remove all excluded 1 The original binary tree of the subtree .
node node The subtree of is node Plus all itself node The offspring of .
Example 1:
Input :root = [1,null,0,0,1]
Output :[1,null,0,null,1]
explain :
Only the red nodes meet the conditions “ All do not contain 1 The subtree of ”. On the right is the answer .
Example 2:
Input :root = [1,0,1,0,0,0,1]
Output :[1,null,1,null,1]
Example 3:
Input :root = [1,1,0,1,1,0,1,0]
Output :[1,1,0,1,1,null,1]
Tips :
The number of nodes in the tree is in the range [1, 200] Inside
Node.val by 0 or 1
Code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
// recursive , Traversing the tree , Edge traversal changes the structure of the tree
public TreeNode pruneTree(TreeNode root) {
if( root == null){
return null;
}
// Judge whether the left and right children of the tree are empty , If it doesn't include 1 The subtree of , Also set it to null
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if(root.left == null && root.right == null && root.val == 0){
return null;
}
return root;
}
}
边栏推荐
- 太空射击 Part 2-2: 敌人精灵
- Numpy:基本操作快速入门
- JVM内存模型简介
- 倍福PLC和C#通过ADS通信传输String类型
- php框架MVC类代码审计
- Intégrité du signal (si) intégrité de l'alimentation électrique (PI) notes d'apprentissage (32) Réseau de distribution d'énergie (4)
- 第十二天笔记
- Current limiting based on redis+lua
- How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
- Beihui information is 12 years old | happy birthday
猜你喜欢
随机推荐
C语言-大端存储和小端存储
Common CMD commands to quickly open programs
成功 万象奥科与CODESYS技术联合调测
软件测试岗位饱和了?自动化测试是新一代‘offer’技能
编译与预处理
Helm installing rancher
Space shooting part 2-2: enemy spirit
信号完整性(SI)电源完整性(PI)学习笔记(三十一)电源分配网路(三)
OpenCV图像处理(中) 图像平滑+直方图
分类模型的评估
Convert the specified seconds to minutes and seconds
倍福PLC--C#ADS通信以通知的方式读取变量
Beifu PLC and C transmit bool type variables through ads communication
Static routing principle and configuration
倍福PLC和C#通过ADS通信传输Bool数组变量
第十一天笔记
倍福PLC和C#通过ADS通信传输String数组类型变量
How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
Intégrité du signal (si) intégrité de l'alimentation électrique (PI) notes d'apprentissage (32) Réseau de distribution d'énergie (4)
【JZOF】10斐波那契数列









