From bb98228305d8deb6b9c2c316fa9fe911dcb12a39 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Tue, 11 Jun 2024 01:52:43 +0900 Subject: [PATCH 1/5] solve : reorder-list --- reorder-list/samthekorean.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 reorder-list/samthekorean.py diff --git a/reorder-list/samthekorean.py b/reorder-list/samthekorean.py new file mode 100644 index 0000000000..d8a294c8fc --- /dev/null +++ b/reorder-list/samthekorean.py @@ -0,0 +1,32 @@ +# TC : O(n) +# SC : O(m) m being the sum of deque and dummy nodes +class Solution: + def reorderList(self, head: Optional[ListNode]) -> None: + if not head or not head.next or not head.next.next: + return + + # Step 1: Use a deque to collect nodes + d = deque() + current = head + while current: + d.append(current) + current = current.next + + # Step 2: Reorder the list using deque + dummy = ListNode(0) # Dummy node with value 0 + current = dummy + toggle = True + + while d: + if toggle: + current.next = d.popleft() # Append from the front of the deque + else: + current.next = d.pop() # Append from the back of the deque + current = current.next + toggle = not toggle + + # Step 3: Ensure the last node points to None + current.next = None + + # Step 4: Reassign head to point to the new reordered list + head = dummy.next From 16f78877141406dd77867b5fa2ccd26382f6a1f3 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Wed, 12 Jun 2024 16:39:58 +0900 Subject: [PATCH 2/5] solve : remove nth node from end of list --- .../samthekorean.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/samthekorean.py diff --git a/remove-nth-node-from-end-of-list/samthekorean.py b/remove-nth-node-from-end-of-list/samthekorean.py new file mode 100644 index 0000000000..1f8f1b0d55 --- /dev/null +++ b/remove-nth-node-from-end-of-list/samthekorean.py @@ -0,0 +1,23 @@ +# TC : O(n) +# SC : O(1) +class Solution: + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + # Create a dummy node to handle edge cases such as removing the first node + dummy = ListNode(0, head) + left = dummy + right = head + + # Move the right pointer n steps ahead + for _ in range(n): + right = right.next + + # Move both pointers until right reaches the end + while right: + left = left.next + right = right.next + + # Remove the nth node from the end + left.next = left.next.next + + # Return the head of the modified list + return dummy.next From 548c334083fbab85c2515b87ab9825d8f69e902f Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Wed, 12 Jun 2024 17:33:08 +0900 Subject: [PATCH 3/5] solve : lowest common ancestor of a binary search tree --- .../samthekorean.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/samthekorean.py diff --git a/lowest-common-ancestor-of-a-binary-search-tree/samthekorean.py b/lowest-common-ancestor-of-a-binary-search-tree/samthekorean.py new file mode 100644 index 0000000000..823cbacd2c --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/samthekorean.py @@ -0,0 +1,16 @@ +# TC : O(log n) because its a balanced tree +# SC : O(1) +class Solution: + def lowestCommonAncestor( + self, root: "TreeNode", p: "TreeNode", q: "TreeNode" + ) -> "TreeNode": + current = root + + # Return when they split to left and right + while current: + if p.val > current.val and q.val > current.val: + current = current.right + elif p.val < current.val and q.val < current.val: + current = current.left + else: + return current From d551167b7b4bfe2dda906a85aa215e18da344bbc Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Wed, 12 Jun 2024 23:12:51 +0900 Subject: [PATCH 4/5] solve : binary tree level order traversal --- .../samthekorean.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 binary-tree-level-order-traversal/samthekorean.py diff --git a/binary-tree-level-order-traversal/samthekorean.py b/binary-tree-level-order-traversal/samthekorean.py new file mode 100644 index 0000000000..285397167f --- /dev/null +++ b/binary-tree-level-order-traversal/samthekorean.py @@ -0,0 +1,24 @@ +# TC : O(n) +# SC : O(n) +class Solution: + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + q = collections.deque() + res = [] + q.append(root) + + while q: + level = [] + qLen = len(q) + + # traverse nodes in each level and append left and right subtree if node is None + for i in range(qLen): + node = q.popleft() + if node: + level.append(node.val) + # append the subtrees of q for the next level in advance + q.append(node.left) + q.append(node.right) + if level: + res.append(level) + + return res From af9a5cfa06ac4f65bd10135060a4b4d61d6e6a2d Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Fri, 14 Jun 2024 01:36:58 +0900 Subject: [PATCH 5/5] solve : validate binary search tree --- validate-binary-search-tree/samthekorean.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 validate-binary-search-tree/samthekorean.py diff --git a/validate-binary-search-tree/samthekorean.py b/validate-binary-search-tree/samthekorean.py new file mode 100644 index 0000000000..5abe860e3f --- /dev/null +++ b/validate-binary-search-tree/samthekorean.py @@ -0,0 +1,16 @@ +# TC : O(n) where n being the number of nodes of the tree +# SC : O(n) where n being the size of sum of every nodes +class Solution: + def isValidBST(self, root: Optional[TreeNode]) -> bool: + def is_valid_bst(node, min_val, max_val): + if not node: + return True + + if node.val <= min_val or node.val >= max_val: + return False + + return is_valid_bst(node.left, min_val, node.val) and is_valid_bst( + node.right, node.val, max_val + ) + + return is_valid_bst(root, float("-inf"), float("inf"))