Skip to content

Commit 69779a7

Browse files
sum of left leaves
1 parent 7feb354 commit 69779a7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎Contest/src/_20160924_6th_contest/SumofLeftLeaves.java renamed to ‎EASY/src/easy/SumofLeftLeaves.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package _20160924_6th_contest;
1+
package easy;
22

33
import classes.TreeNode;
44

@@ -26,4 +26,21 @@ private int dfs(TreeNode root, int result, boolean left) {
2626
}
2727
return leftResult + rightResult;
2828
}
29+
30+
private class Solution_more_concise{
31+
32+
public int sumOfLeftLeaves(TreeNode root) {
33+
int sum = 0;
34+
if(root == null) return sum;
35+
if(root.left != null){
36+
if(root.left.left == null && root.left.right == null) sum += root.left.val;
37+
else sum += sumOfLeftLeaves(root.left);
38+
}
39+
if(root.right != null){
40+
sum += sumOfLeftLeaves(root.right);
41+
}
42+
return sum;
43+
}
44+
45+
}
2946
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../../blob/master/MEDIUM/src/medium/BattleshipsinaBoard.java) | O(n^2) |O(1) | Medium| DFS
66
|415|[Add Strings](https://leetcode.com/problems/add-strings/)|[Solution](../../blob/master/EASY/src/easy/AddStrings.java)| O(n)|O(1) | Easy|
77
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[Solution](../../blob/master/EASY/src/easy/FizzBuzz.java)| O(n)|O(1) | Easy|
8+
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/)|[Solution](../../blob/master/EASY/src/easy/SumofLeftLeaves.java)| O(n)|O(h) | Easy|
89
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[Solution](../../blob/master/MEDIUM/src/medium/RandomPickIndex.java) | | | Medium| Reservoir Sampling
910
|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[Solution](../../blob/master/EASY/src/easy/IntegerReplacement.java)| ? | ? | Easy| BFS
1011
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|[Solution](../../blob/master/EASY/src/easy/RotateFunction.java)| O(n^2) could be optimized to O(n) | O(1) | Easy|

0 commit comments

Comments
 (0)