当前位置:网站首页>[LeetCode]404. Sum of left leaves
[LeetCode]404. Sum of left leaves
2022-07-03 06:42:00 【Lost leaf】
This is my answer now ! 2022 year 6 month 26 Japan 22:33:43
It just took me less than estimated 10min make .
public int sumOfLeftLeaves(TreeNode root) {
// At first, I still think of using recursion 2022 year 6 month 26 Japan 22:23:46
if(root == null){
return 0;
}
int result = 0;
TreeNode left = root.left;
TreeNode right = root.right;
if(left != null){
if(left.left == null && left.right == null){
result += left.val;
}else{
result += sumOfLeftLeaves(left);
}
if(left.left != null){
}
}
result += sumOfLeftLeaves(right);
return result;
}
Then I found that my original code was too bloated , I can't see it at all !
Poor readability !
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 0;
}
// Leaf node A node without children .
int sum = 0;
sum += sumByRecusiveSelf( root.left, true);
sum += sumByRecusiveSelf( root.right, false);
return sum;
}
private int sumByRecusiveSelf(TreeNode node, boolean isLeft) {
if (node == null) {
return 0;
}
if (node.left == null && node.right == null) {
// The left leaf just returns => Added to the result
if (isLeft) {
return node.val;
} else {
return 0;
}
}
int result = 0;
result += sumByRecusiveSelf(node.left, true);
result += sumByRecusiveSelf(node.right, false);
return result;
}
Do you want to judge whether the left node , You don't need it at all , direct null You can also use recursive methods , It will return anyway 0 ah !
Look at the solution !
Method two doesn't go away , The stack is used .
Method 1 is actually the same as my current writing , Take a good look .
complete .
Method 1 : Depth-first search
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
return root != null ? dfs(root) : 0;
}
public int dfs(TreeNode node) {
int ans = 0;
if (node.left != null) {
ans += isLeafNode(node.left) ? node.left.val : dfs(node.left);
}
if (node.right != null && !isLeafNode(node.right)) {
ans += dfs(node.right);
}
return ans;
}
public boolean isLeafNode(TreeNode node) {
return node.left == null && node.right == null;
}
}
边栏推荐
- scroll-view指定滚动元素的起始位置
- Shell conditional statement
- Chapter 8. MapReduce production experience
- 论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》
- opencv鼠标键盘事件
- JMeter linked database
- Mysql database
- opencv
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- Operation principle of lua on C: Foundation
猜你喜欢

Scripy learning
![[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)](/img/a4/00aca72b268f77fe4fb24ac06289f5.jpg)
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)

Une exploration intéressante de l'interaction souris - pointeur

Golang operation redis: write and read hash type data

表达式的动态解析和计算,Flee用起来真香

IE browser flash back, automatically open edge browser

Selenium - by changing the window size, the width, height and length of different models will be different

2022 CISP-PTE(三)命令执行

Support vector machine for machine learning

第8章、MapReduce 生产经验
随机推荐
23 design models
Create your own deep learning environment with CONDA
Ruoyi interface permission verification
卡特兰数(Catalan)的应用场景
【5G NR】UE注册流程
Mysql
Numerical method for solving optimal control problem (I) -- gradient method
Selenium ide installation recording and local project maintenance
A letter to graduating college students
Use selenium to climb the annual box office of Yien
ssh链接远程服务器 及 远程图形化界面的本地显示
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
Chapter 8. MapReduce production experience
Yolov2 learning and summary
The difference between CONDA and pip
Difference between shortest path and minimum spanning tree
Daily question brushing record (11)
使用conda创建自己的深度学习环境
error C2017: 非法的转义序列
Reinstalling the system displays "setup is applying system settings" stationary