Skip to content

Commit 7226d68

Browse files
solves sum left leaves in python
1 parent abb6b27 commit 7226d68

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
| 389 | [Find the Difference](https://leetcode.com/problems/find-the-difference) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/FindTheDifference.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/find_the_difference.py) |
108108
| 392 | [Is Subsequence](https://leetcode.com/problems/is-subsequence) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IsSubsequence.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/is_subsequence.py) |
109109
| 401 | [Binary Watch](https://leetcode.com/problems/binary-watch) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/BinaryWatch.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/binary_watch.py) |
110-
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/SumOfLeftLeaves.java) |
110+
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/SumOfLeftLeaves.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/sum_of_left_leaves.py) |
111111
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ConvertNumberToHexadecimal.java) |
112112
| 408 | [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation) | Easy | |
113113
| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/LongestPalindrome.java) |

python/sum_of_left_leaves.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
class Solution:
10+
def is_leaf_node(self, root: TreeNode) -> bool:
11+
return root.left is None and root.right is None
12+
13+
def sum_left_leaves(self, root: TreeNode, is_left_branch: bool) -> int:
14+
if root is None:
15+
return 0
16+
if self.is_leaf_node(root) and is_left_branch:
17+
return root.val
18+
return self.sum_left_leaves(root.left, is_left_branch=True) + self.sum_left_leaves(root.right, is_left_branch=False)
19+
20+
def sumOfLeftLeaves(self, root: TreeNode) -> int:
21+
return self.sum_left_leaves(root, is_left_branch=False)

0 commit comments

Comments
 (0)