Skip to content

Commit a9412d4

Browse files
committed
左叶子之和
1 parent 6cc231e commit a9412d4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
| 377 | 动态规划 | [组合总和 Ⅳ](src/main/java/algorithm/leetcode/Solution377.java) | 中等 |
131131
| 392 | 动态规划、贪心算法、二分查找 | [判断子序列](src/main/java/algorithm/leetcode/Solution392.java) | 中等 |
132132
| 402 | 栈、贪心算法 | [移掉K位数字](src/main/java/algorithm/leetcode/Solution402.java) | 中等 |
133+
| 404 || [左叶子之和](src/main/java/algorithm/leetcode/Solution404.java) | 简单 |
133134
| 406 | 贪心算法 | [根据身高重建队列](src/main/java/algorithm/leetcode/Solution406.java) | 中等 |
134135
| 413 | 数学、动态规划 | [等差数列划分](src/main/java/algorithm/leetcode/Solution413.java) | 中等 |
135136
| 416 | 动态规划 | [分割等和子集](src/main/java/algorithm/leetcode/Solution416.java) | 中等 |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package algorithm.leetcode;
2+
3+
/**
4+
* @author: mayuan
5+
* @desc: 左叶子之和
6+
* @date: 2019/03/10
7+
*/
8+
public class Solution404 {
9+
public int sumOfLeftLeaves(TreeNode root) {
10+
if (null == root){
11+
return 0;
12+
}
13+
14+
return dfs(root.left, true) + dfs(root.right, false);
15+
}
16+
17+
private int dfs(TreeNode node, boolean isLeft) {
18+
if (null == node) {
19+
return 0;
20+
}
21+
22+
if (null == node.left && null == node.right && isLeft) {
23+
return node.val;
24+
} else {
25+
return dfs(node.left, true) + dfs(node.right, false);
26+
}
27+
}
28+
29+
public class TreeNode {
30+
int val;
31+
TreeNode left;
32+
TreeNode right;
33+
34+
TreeNode(int x) {
35+
val = x;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)