Skip to content

Commit dee9e83

Browse files
authored
Merge pull request #128 from SamTheKorean/solution7
[SAM] Week 7 solutions
2 parents bcb2945 + af9a5cf commit dee9e83

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# TC : O(n)
2+
# SC : O(n)
3+
class Solution:
4+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
5+
q = collections.deque()
6+
res = []
7+
q.append(root)
8+
9+
while q:
10+
level = []
11+
qLen = len(q)
12+
13+
# traverse nodes in each level and append left and right subtree if node is None
14+
for i in range(qLen):
15+
node = q.popleft()
16+
if node:
17+
level.append(node.val)
18+
# append the subtrees of q for the next level in advance
19+
q.append(node.left)
20+
q.append(node.right)
21+
if level:
22+
res.append(level)
23+
24+
return res
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# TC : O(log n) because its a balanced tree
2+
# SC : O(1)
3+
class Solution:
4+
def lowestCommonAncestor(
5+
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
6+
) -> "TreeNode":
7+
current = root
8+
9+
# Return when they split to left and right
10+
while current:
11+
if p.val > current.val and q.val > current.val:
12+
current = current.right
13+
elif p.val < current.val and q.val < current.val:
14+
current = current.left
15+
else:
16+
return current
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
5+
# Create a dummy node to handle edge cases such as removing the first node
6+
dummy = ListNode(0, head)
7+
left = dummy
8+
right = head
9+
10+
# Move the right pointer n steps ahead
11+
for _ in range(n):
12+
right = right.next
13+
14+
# Move both pointers until right reaches the end
15+
while right:
16+
left = left.next
17+
right = right.next
18+
19+
# Remove the nth node from the end
20+
left.next = left.next.next
21+
22+
# Return the head of the modified list
23+
return dummy.next

reorder-list/samthekorean.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# TC : O(n)
2+
# SC : O(m) m being the sum of deque and dummy nodes
3+
class Solution:
4+
def reorderList(self, head: Optional[ListNode]) -> None:
5+
if not head or not head.next or not head.next.next:
6+
return
7+
8+
# Step 1: Use a deque to collect nodes
9+
d = deque()
10+
current = head
11+
while current:
12+
d.append(current)
13+
current = current.next
14+
15+
# Step 2: Reorder the list using deque
16+
dummy = ListNode(0) # Dummy node with value 0
17+
current = dummy
18+
toggle = True
19+
20+
while d:
21+
if toggle:
22+
current.next = d.popleft() # Append from the front of the deque
23+
else:
24+
current.next = d.pop() # Append from the back of the deque
25+
current = current.next
26+
toggle = not toggle
27+
28+
# Step 3: Ensure the last node points to None
29+
current.next = None
30+
31+
# Step 4: Reassign head to point to the new reordered list
32+
head = dummy.next
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# TC : O(n) where n being the number of nodes of the tree
2+
# SC : O(n) where n being the size of sum of every nodes
3+
class Solution:
4+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
5+
def is_valid_bst(node, min_val, max_val):
6+
if not node:
7+
return True
8+
9+
if node.val <= min_val or node.val >= max_val:
10+
return False
11+
12+
return is_valid_bst(node.left, min_val, node.val) and is_valid_bst(
13+
node.right, node.val, max_val
14+
)
15+
16+
return is_valid_bst(root, float("-inf"), float("inf"))

0 commit comments

Comments
 (0)