Skip to content

Commit 1f740bf

Browse files
committed
[add] LeetCode 404. Sum of Left Leaves
1 parent d6378f3 commit 1f740bf

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

algorithms-leetcode/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Leetcode problems classified by company:
9999
|354|Russian Doll Envelopes|Hard|||
100100
|376|Wiggle Subsequence|Medium|Greedy/Dynamic Programming|TODO 一题多解|
101101
|392|Is Subsequence|Easy|Dynamic Programming/Two Pointers|一题多解|
102+
|404|Sum of Left Leaves|Easy|Binary Tree||
102103
|416|Partition Equal Subset Sum|Medium|Dynamic Programming,01背包||
103104
|455|Assign Cookies|Easy|Greedy||
104105
|474|Ones and Zeroes|Medium|Dynamic Programming,01背包|TODO 二维解法|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.brianway.learning.algorithms.leetcode.easy;
2+
3+
import com.brianway.learning.algorithms.leetcode.common.TreeNode;
4+
5+
/**
6+
* LeetCode 404. Sum of Left Leaves
7+
* Question https://leetcode.com/problems/sum-of-left-leaves/
8+
* 关键题设:A leaf is a node with no children.
9+
* A left leaf is a leaf that is the left child of another node.
10+
*
11+
* @auther brian
12+
* @since 2022/9/6 00:00
13+
*/
14+
public class SumOfLeftLeaves {
15+
16+
public int sumOfLeftLeaves(TreeNode root) {
17+
return 0;
18+
}
19+
20+
/**
21+
* 递归
22+
* 后序遍历
23+
*/
24+
public class SumOfLeftLeaves0 extends SumOfLeftLeaves {
25+
@Override
26+
public int sumOfLeftLeaves(TreeNode root) {
27+
// 终止条件
28+
if (root == null) {
29+
return 0;
30+
}
31+
// 单层逻辑
32+
int leftSum = sumOfLeftLeaves(root.left);
33+
if (root.left != null && root.left.left == null && root.left.right == null) {
34+
// 仅这个if条件下才会有val值贡献进来
35+
leftSum = root.left.val;
36+
}
37+
int rightSum = sumOfLeftLeaves(root.right);
38+
39+
return leftSum + rightSum;
40+
}
41+
}
42+
43+
}
44+

0 commit comments

Comments
 (0)