当前位置:网站首页>Force deduction solution summary 814 binary tree pruning
Force deduction solution summary 814 binary tree pruning
2022-07-22 19:32: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;
}
}
边栏推荐
- Unity: material download
- This points to the problem
- Go 中的基础库Time时间处理
- 30出头成为复旦博导,陈思明:敲代码和写诗,我两样都要
- Laravel solves [1045] access denied for user 'homestead' @ 'localhost' (usin g password: yes)
- Enumerate properties in objects
- Grafana panel - override field values
- The force deduction method summarizes the k-diff number pairs in the 532 array
- arguments
- LeetCode 每日一题 2021/12/13-2021/12/19
猜你喜欢
Grafana panel - override field values
MFC dialog program only runs a simple example of a single instance
Scenario practice | how to use rongyun super group to build a game community
Day3:分支结构
2022版Centos8 yum镜像安装&阿里云安装Mysql 5.7教程与问题解决
Swagger-UI介绍及常用注解说明
Laravel solves [1045] access denied for user 'homestead' @ 'localhost' (usin g password: yes)
Several trends in the future development of software industry
This points to the problem
Help brand insight -- Analysis of consumers' emotional behavior
随机推荐
融云漫话:没有一个人躲得过“视频会议”
音频 3A 处理实践,让你的应用更「动听」
Global scope and function scope
Rongyun handles political affairs: "small grid" can also achieve "big governance"
软件产业未来发展的几个趋势
Day4:循环结构
Introduction to date object
Ps: how to call up auxiliary lines
LeetCode 每日一题 2022/1/3-2022/1/9
mysql5.7解压版配置步骤
LeetCode 每日一题 2022/1/31-2022/2/6
STM32中什么是预分频
Constructor
"35 years old, I retired": This is the most reliable answer to the midlife crisis
DOM introduction and query
场景实践 | 如何使用融云超级群构建游戏社区
什么是“实时”
Ten year structure five year Life-05 first business trip
CentOS7安装Mysql5.7解压版&Navicat连接Mysql&防火墙设置——亲测有效
Execute function now