-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution404.java
More file actions
38 lines (32 loc) · 773 Bytes
/
Solution404.java
File metadata and controls
38 lines (32 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 左叶子之和
* @date: 2019/03/10
*/
public class Solution404 {
public int sumOfLeftLeaves(TreeNode root) {
if (null == root){
return 0;
}
return dfs(root.left, true) + dfs(root.right, false);
}
private int dfs(TreeNode node, boolean isLeft) {
if (null == node) {
return 0;
}
if (null == node.left && null == node.right && isLeft) {
return node.val;
} else {
return dfs(node.left, true) + dfs(node.right, false);
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}