Skip to content

Commit

Permalink
Apr 14
Browse files Browse the repository at this point in the history
  • Loading branch information
siddydutta committed Apr 14, 2024
1 parent 1b2c359 commit 3fe182a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions 2024-04-April-LeetCoding-Challenge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
| April 11 | [402. Remove K Digits](https://leetcode.com/problems/remove-k-digits/) | Medium | Unsolved |
| April 12 | []() | | |
| April 13 | []() | | |
| April 14 | []() | | |
| April 14 | [404. Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | Easy | Solved |
| April 15 | []() | | |
| April 16 | []() | | |
| April 17 | []() | | |
Expand All @@ -38,6 +38,6 @@
## Summary
| Level | Problems | Solved | Unsolved |
| --- | --- | --- | --- |
| Easy | 6 | 6 | 0 |
| Easy | 7 | 7 | 0 |
| Medium | 5 | 2 | 3 |
| Hard | 0 | 0 | 0 |
42 changes: 42 additions & 0 deletions 2024-04-April-LeetCoding-Challenge/Sum of Left Leaves.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Optional


# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
queue = [root]
total_sum = 0
while queue:
next_queue = []
for node in queue:
if node.left:
if not node.left.left and not node.left.right:
total_sum += node.left.val
next_queue.append(node.left)
if node.right:
next_queue.append(node.right)
queue = next_queue
return total_sum


def main():
root = TreeNode(3)
root.left = TreeNode(9)
root.right = TreeNode(20)
root.right.left = TreeNode(15)
root.right.right = TreeNode(7)
assert Solution().sumOfLeftLeaves(root) == 24

root = TreeNode(1)
assert Solution().sumOfLeftLeaves(root) == 0


if __name__ == '__main__':
main()

0 comments on commit 3fe182a

Please sign in to comment.