File tree 5 files changed +81
-1
lines changed
algorithms/SumOfLeftLeaves
5 files changed +81
-1
lines changed Original file line number Diff line number Diff line change @@ -33,4 +33,5 @@ All solutions will be accepted!
33
33
| 191| [ Number Of 1 Bits] ( https://leetcode-cn.com/problems/number-of-1-bits/description/ ) | [ java/py/js] ( ./algorithms/NumberOf1Bits ) | Easy|
34
34
| 258| [ Add Digits] ( https://leetcode-cn.com/problems/add-digits/description/ ) | [ java/py/js] ( ./algorithms/AddDigits ) | Easy|
35
35
| 520| [ Detect Capital] ( https://leetcode-cn.com/problems/detect-capital/description/ ) | [ java/py/js] ( ./algorithms/DetectCaptial ) | Easy|
36
- | 682| [ BaseBall Game] ( https://leetcode-cn.com/problems/baseball-game/description/ ) | [ java/py/js] ( ./algorithms/BaseBallGame ) | Easy|
36
+ | 682| [ BaseBall Game] ( https://leetcode-cn.com/problems/baseball-game/description/ ) | [ java/py/js] ( ./algorithms/BaseBallGame ) | Easy|
37
+ | 404| [ Sum Of Left Leaves] ( https://leetcode-cn.com/problems/sum-of-left-leaves/description/ ) | [ java/py/js] ( ./algorithms/SumOfLeftLeaves ) | Easy|
Original file line number Diff line number Diff line change
1
+ # Sum Of Left Leaves
2
+ We can use recursion to solve this problem
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ public int sumOfLeftLeaves (TreeNode root ) {
12
+ if (root == null ) {
13
+ return 0 ;
14
+ } else if (root .left == null && root .right == null ) {
15
+ return 0 ;
16
+ }
17
+ return sumOfLeftLeaves (root .left , true ) + sumOfLeftLeaves (root .right , false );
18
+ }
19
+
20
+ public int sumOfLeftLeaves (TreeNode node , boolean isLeft ) {
21
+ if (node == null ) {
22
+ return 0 ;
23
+ } else if (node .left == null && node .right == null && isLeft ) {
24
+ return node .val ;
25
+ }
26
+ return sumOfLeftLeaves (node .left , true ) + sumOfLeftLeaves (node .right , false );
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val) {
4
+ * this.val = val;
5
+ * this.left = this.right = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {TreeNode } root
10
+ * @return {number }
11
+ */
12
+ var sumOfLeftLeaves = function ( root ) {
13
+ if ( ! root ) return 0
14
+ if ( ! root . left && ! root . right ) return 0
15
+ return sumOfLeftLeavesx ( root . left , true ) + sumOfLeftLeavesx ( root . right , false )
16
+ } ;
17
+
18
+ function sumOfLeftLeavesx ( node , isLeft ) {
19
+ if ( ! node ) return 0
20
+ if ( ! node . left && ! node . right && isLeft ) return node . val
21
+ return sumOfLeftLeavesx ( node . left , true ) + sumOfLeftLeavesx ( node . right , false )
22
+ }
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.left = None
6
+ # self.right = None
7
+
8
+ class Solution (object ):
9
+ def sumOfLeftLeaves (self , root ):
10
+ """
11
+ :type root: TreeNode
12
+ :rtype: int
13
+ """
14
+ if not root :
15
+ return 0
16
+ if not root .left and not root .right :
17
+ # root is not left leave
18
+ return 0
19
+ return self .sumOfLeftLeavesx (root .left ) + self .sumOfLeftLeavesx (root .right , is_left = False )
20
+
21
+ def sumOfLeftLeavesx (self , node , is_left = True ):
22
+ if not node :
23
+ return 0
24
+ if not node .left and not node .right and is_left :
25
+ return node .val
26
+ return self .sumOfLeftLeavesx (node .left ) + self .sumOfLeftLeavesx (node .right , is_left = False )
27
+
You can’t perform that action at this time.
0 commit comments