当前位置:网站首页>[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;
}
}
边栏推荐
猜你喜欢

Ruoyi interface permission verification

Reinstalling the system displays "setup is applying system settings" stationary

These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products

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

Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface

Chapter 8. MapReduce production experience

Redis cluster creation, capacity expansion and capacity reduction

100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
![[classes and objects] explain classes and objects in simple terms](/img/41/250457530880dfe3728432c2ccd50b.png)
[classes and objects] explain classes and objects in simple terms

Golang operation redis: write and read kV data
随机推荐
ssh链接远程服务器 及 远程图形化界面的本地显示
论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》
认识弹性盒子flex
opencv
【类和对象】深入浅出类和对象
Opencv mouse and keyboard events
2022 cisp-pte (III) command execution
ROS+Pytorch的联合使用示例(语义分割)
After the Chrome browser is updated, lodop printing cannot be called
Climb movie paradise 2021 hot
The difference between CONDA and pip
golang操作redis:写入、读取kv数据
Various usages of MySQL backup database to create table select and how many days are left
致即将毕业大学生的一封信
A letter to graduating college students
Introduction to software engineering
Local rviz call and display of remote rostopic
Difference between shortest path and minimum spanning tree
HMS core helps baby bus show high-quality children's digital content to global developers
Openresty best practices