当前位置:网站首页>[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;
}
}
边栏推荐
- Yolov2 learning and summary
- HMS core helps baby bus show high-quality children's digital content to global developers
- Modify MySQL password
- Climb movie paradise 2021 hot
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- Operation principle of lua on C: Foundation
- Mysql database binlog log enable record
- scroll-view指定滚动元素的起始位置
- Heap sort and priority queue
- Personally design a highly concurrent seckill system
猜你喜欢

Scroll view specifies the starting position of the scrolling element

每日刷题记录 (十一)

JMeter performance automation test

Golang operation redis: write and read hash type data

有意思的鼠標指針交互探究

Golang operation redis: write and read kV data

Example of joint use of ros+pytoch (semantic segmentation)

golang操作redis:写入、读取kv数据
![[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)

卡特兰数(Catalan)的应用场景
随机推荐
代码管理工具
Learning notes -- principles and comparison of k-d tree and IKD tree
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
Support vector machine for machine learning
A letter to graduating college students
修改MySQL密码
opencv鼠标键盘事件
Page text acquisition
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
(翻译)异步编程:Async/Await在ASP.NET中的介绍
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
Reinstalling the system displays "setup is applying system settings" stationary
SQL实现将多行记录合并成一行
New knowledge! The virtual machine network card causes your DNS resolution to slow down
POI dealing with Excel learning
Unittest attempt
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
[classes and objects] explain classes and objects in simple terms
Mysql database
YOLOV1学习笔记