当前位置:网站首页>Force deduction solution summary 814 binary tree pruning
Force deduction solution summary 814 binary tree pruning
2022-07-22 13:05:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/binary-tree-pruning
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * The idea of recursion , * Every time you recurse , Judge separately left,right, It does not contain 1, If none , Then return to false. Instead, return to true. * The upper layer receives false when , Delete this node .
Code :
public class Solution814 {
public TreeNode pruneTree(TreeNode root) {
boolean b = have1Node(root);
return !b ? null : root;
}
/**
* root Contained in the 1 Then return to true, Otherwise false
*
* @param root
* @return
*/
public boolean have1Node(TreeNode root) {
TreeNode left = root.left;
TreeNode right = root.right;
boolean result = false;
if (left != null) {
if (have1Node(left)) {
result = true;
} else {
root.left = null;
}
}
if (right != null) {
if (have1Node(right)) {
result = true;
} else {
root.right = null;
}
}
return result || root.val == 1;
}
}
边栏推荐
- 小程序毕设作品之微信酒店预订小程序毕业设计(2)小程序功能
- Md5&Md5盐值加密
- 2022 Shandong Health Exhibition, health industry exhibition, DJK China Health Expo, sleep health exhibition
- Explore the implementation of hash table through redis source code
- threejs
- 《六》BFC
- Force buckle to distribute biscuits
- PIP common commands
- 使用JDBC操作数据库数据库操作
- 力扣上的行星碰撞问题(函数递归方法)
猜你喜欢
The sandbox and tower team jointly launched the tower defense game creation competition
OAuth2.0
ECCV 2022 | 基于不变特征学习的广义长尾分类
97 page digital twin enabled smart Park IOT cloud platform construction scheme
C # read and write data to PLC in conjunction with kepserver
2022.07.20
STM32 minimum system design
小程序毕设作品之微信酒店预订小程序毕业设计(4)开题报告
Aike AI frontier promotion (7.21)
Mongodb learning journey
随机推荐
121. The best time to buy and sell stocks
Configure SSL certificate for Baidu cloud virtual machine BCH
小程序毕设作品之微信酒店预订小程序毕业设计(3)后台功能
透过Redis源码探究Hash表的实现
《6》 BFC
Four redis cluster schemes and their advantages and disadvantages
C# 联合KepServer对PLC进行数据读写
Multifunctional embedded decoding software (1)
Use response in the requests library Json() error: json decoder. JSONDecodeError: Expecting value
4种 Redis 集群方案及优缺点对比
Multidimensional track, best selection | cloud store hot list in July
Format data JSON in pycharm tool
Low power underwater Internet of things: vision and key technologies
OAuth2.0
pycharm工具中将数据json格式化
ECCV 2022 | generalized long tail classification based on invariant feature learning
IOOutputStream类型
【技术】uniapp 之引入 u-charts 部分demo
不同实现方式的球幕投影都有哪些特点
2022 音视频技术风向标