-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
28 lines (27 loc) · 799 Bytes
/
Solution.java
File metadata and controls
28 lines (27 loc) · 799 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
} else if (root.left == null && root.right == null) {
return 0;
}
return sumOfLeftLeaves(root.left, true) + sumOfLeftLeaves(root.right, false);
}
public int sumOfLeftLeaves(TreeNode node, boolean isLeft) {
if (node == null) {
return 0;
} else if (node.left == null && node.right == null && isLeft) {
return node.val;
}
return sumOfLeftLeaves(node.left, true) + sumOfLeftLeaves(node.right, false);
}
}