diff --git a/maximum-depth-of-binary-tree/mandel-17.py b/maximum-depth-of-binary-tree/mandel-17.py new file mode 100644 index 0000000000..a15a029561 --- /dev/null +++ b/maximum-depth-of-binary-tree/mandel-17.py @@ -0,0 +1,26 @@ +import collections +from typing import Optional + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + queue = collections.deque([root]) + depth = 0 + + while queue: + depth += 1 + for _ in range(len(queue)): + current_root = queue.popleft() + if current_root.left: + queue.append(current_root.left) + if current_root.right: + queue.append(current_root.right) + return depth + diff --git a/merge-two-sorted-lists/mandel-17.py b/merge-two-sorted-lists/mandel-17.py new file mode 100644 index 0000000000..828864ad45 --- /dev/null +++ b/merge-two-sorted-lists/mandel-17.py @@ -0,0 +1,16 @@ +from typing import Optional + +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + if (not list1) or (list2 and list1.val > list2.val): + list1, list2 = list2, list1 + if list1: + list1.next = self.mergeTwoLists(list1.next, list2) + + return list1 +