当前位置:网站首页>[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;
}
}
边栏推荐
- golang操作redis:写入、读取hash类型数据
- 简易密码锁
- Nacos service installation
- Yolov3 learning notes
- Application scenarios of Catalan number
- 保险公司怎么查高血压?
- Decision tree of machine learning
- Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
- Pytorch exercise items
- [untitled] 5 self use history
猜你喜欢
Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
Create your own deep learning environment with CONDA
SSH link remote server and local display of remote graphical interface
[5g NR] UE registration process
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
2022年华东师范大学计科考研复试机试题-详细题解
JMeter performance automation test
vmware虚拟机C盘扩容
golang操作redis:写入、读取kv数据
Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
随机推荐
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
opencv鼠标键盘事件
SQL实现将多行记录合并成一行
Use selenium to climb the annual box office of Yien
The difference between CONDA and pip
Time format record
Summary of UI module design and practical application of agent mode
conda和pip的区别
【类和对象】深入浅出类和对象
Pytest attempts to execute the test case without skipping, but the case shows that it is all skipped
Application scenarios of Catalan number
Scripy learning
Ruoyi interface permission verification
【无标题】5 自用历程
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
UTC时间、GMT时间、CST时间
Scroll view specifies the starting position of the scrolling element
Important knowledge points of redis
UNI-APP中条件注释 实现跨段兼容、导航跳转 和 传参、组件创建使用和生命周期函数
论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》