diff --git a/binary-tree-level-order-traversal/Leo.py b/binary-tree-level-order-traversal/Leo.py new file mode 100644 index 000000000..9c12cb828 --- /dev/null +++ b/binary-tree-level-order-traversal/Leo.py @@ -0,0 +1,25 @@ +class Solution: + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + if root is None: + return [] + + queue = deque([root]) + return_list = [] + + while queue: + level_size = len(queue) + current_level = [] + + for n in range(level_size): + node = queue.popleft() + current_level.append(node.val) + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + + return_list.append(current_level) + + return return_list + + ## TC: O(n), SC: O(n) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/Leo.py b/lowest-common-ancestor-of-a-binary-search-tree/Leo.py new file mode 100644 index 000000000..cacd5fa46 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/Leo.py @@ -0,0 +1,14 @@ +class Solution: + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + + while True: + if root.val < min(p.val, q.val): + root = root.right + + elif root.val > max(p.val, q.val): + root = root.left + + else: + return root + + # TC: O(n) where n is height of the tree, SC: O(1) diff --git a/remove-nth-node-from-end-of-list/Leo.py b/remove-nth-node-from-end-of-list/Leo.py new file mode 100644 index 000000000..967be6cbc --- /dev/null +++ b/remove-nth-node-from-end-of-list/Leo.py @@ -0,0 +1,19 @@ +class Solution: + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + + left = right = head + + for i in range(n): + right = right.next + + if not right: return head.next + + while right.next: + left = left.next + right = right.next + + left.next = left.next.next ## deleting node + + return head + + # TC: O(n), SC: O(1) diff --git a/reorder-list/Leo.py b/reorder-list/Leo.py new file mode 100644 index 000000000..0f74c91ef --- /dev/null +++ b/reorder-list/Leo.py @@ -0,0 +1,23 @@ +class Solution: + def reorderList(self, head: Optional[ListNode]) -> None: + """ + Do not return anything, modify head in-place instead. + """ + if not head: + return + + slow, fast = head, head + while fast and fast.next: + slow, fast = slow.next, fast.next.next + + prev, curr = None, slow + + while curr: + curr.next, prev, curr = prev, curr, curr.next + + first, second = head, prev + while second.next: + first.next, first = second, first.next + second.next, second = first, second.next + + ## TC: O(n), SC: O(1) diff --git a/validate-binary-search-tree/Leo.py b/validate-binary-search-tree/Leo.py new file mode 100644 index 000000000..63658cfc1 --- /dev/null +++ b/validate-binary-search-tree/Leo.py @@ -0,0 +1,12 @@ +class Solution: + def isValidBST(self, root: Optional[TreeNode]) -> bool: + def helper(node, low, high): + if not node: + return True + if not (low < node.val < high): + return False + return helper(node.left, low, node.val) and helper(node.right, node.val, high) + + return helper(root, -inf, inf) + + ## TC: O(n), SC: O(n)