当前位置:网站首页>[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;
}
}
边栏推荐
- UTC time, GMT time, CST time
- Code management tools
- [Code] occasionally take values, judge blanks, look up tables, verify, etc
- The most classic 100 sentences in the world famous works
- HMS core helps baby bus show high-quality children's digital content to global developers
- 远端rostopic的本地rviz调用及显示
- Operation principle of lua on C: Foundation
- Mysql
- 使用conda创建自己的深度学习环境
- Redis cluster creation, capacity expansion and capacity reduction
猜你喜欢
golang操作redis:写入、读取hash类型数据
Support vector machine for machine learning
The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
有意思的鼠標指針交互探究
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
golang操作redis:写入、读取kv数据
How to scan when Canon c3120l is a network shared printer
Scripy learning
数值法求解最优控制问题(一)——梯度法
随机推荐
[C /vb.net] convert PDF to svg/image, svg/image to PDF
保险公司怎么查高血压?
Pdf files can only print out the first page
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)
[Code] occasionally take values, judge blanks, look up tables, verify, etc
剖析虚幻渲染体系(16)- 图形驱动的秘密
Yolov3 learning notes
Dbnet: real time scene text detection with differentiable binarization
冒泡排序的简单理解
YOLOV1学习笔记
Some thoughts on machine learning
Selenium ide installation recording and local project maintenance
Golang operation redis: write and read kV data
[untitled] 8 simplified address book
Journal quotidien des questions (11)
POI dealing with Excel learning
简易密码锁
Use selenium to climb the annual box office of Yien
【code】偶尔取值、判空、查表、验证等
scroll-view指定滚动元素的起始位置