-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumofLeftLeaves.java
More file actions
38 lines (33 loc) · 870 Bytes
/
SumofLeftLeaves.java
File metadata and controls
38 lines (33 loc) · 870 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 easy;
import util.TreeNode;
/**
* ClassName: SumofLeftLeaves.java
* Author: chenyiAlone
* Create Time: 2019/8/5 23:56
* Description: No.404 Sum of Left Leaves
*
*Find the sum of all left leaves in a given binary tree.
*
* Example:
*
* 3
* / \
* 9 20
* / \
* 15 7
*
* There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
*/
public class SumofLeftLeaves {
private int dfs(TreeNode root, boolean isLeft) {
int ret = 0;
if (root == null) return 0;
if (root.left == null && root.right == null && isLeft) return root.val;
if (root.left != null) ret += dfs(root.left, true);
if (root.right != null) ret += dfs(root.right, false);
return ret;
}
public int sumOfLeftLeaves(TreeNode root) {
return dfs(root, false);
}
}