Skip to content

Commit bf8fa51

Browse files
authored
Initial Commit
1 parent 49ea549 commit bf8fa51

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed

coding_2025/graphs/find_cycle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from collections import defaultdict
66

7-
WHITE, GRAY, BLACK = 0, 1,2
7+
WHITE, GRAY, BLACK = 0, 1, 2
88

99
def has_cycle(graph):
1010
color = defaultdict(lambda: WHITE)

coding_2025/meta/lc_251.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def quickselect(nums: List[int], left: int, right: int, k: int) -> int:
3737
if pivot_index < n - k:
3838
# Recurse right
3939
return quickselect(nums, pivot_index + 1, right, k)
40-
elif pivot_index > n - k:
41-
# Recurse left
42-
return quickselect
40+
elif pivot_index > n- k:
41+
return quickselect(nums, left, pivot_index - 1, k)
42+
else:
43+
return nums[pivot_index]
44+
45+
return quickselect(nums, 0, len(nums) - 1, k)

coding_2025/meta/lc_430.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,47 @@
55

66
"""
77
# Definition for a Node.
8-
class Node:
8+
class Node(object):
99
def __init__(self, val, prev, next, child):
1010
self.val = val
1111
self.prev = prev
1212
self.next = next
1313
self.child = child
1414
"""
1515

16-
class Solution:
17-
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
16+
class Solution(object):
17+
def flatten(self, head):
1818
if not head:
19-
return head # If the list is empty, just return
19+
return None # Return if the list is empty
2020

21-
def helper(curr):
22-
"""
23-
Recursively flattens the list starting from `curr`.
24-
Returns the last node of the flattened list.
25-
"""
26-
last = curr # To track the tail of the flattened portion
21+
# Create a dummy node to simplify edge cases
22+
pseudoHead = Node(0, None, head, None)
23+
prev = pseudoHead
2724

28-
while curr:
29-
tempNext = curr.next # Save next pointer in case we overwrite it
25+
stack = [head] # Use a stack for depth-first traversal
3026

31-
if curr.child:
32-
# Save the child head
33-
child_head = curr.child
27+
while stack:
28+
curr = stack.pop() # Pop the current node to process
3429

35-
# Recursively flatten the child list, returns the tail of the child chain
36-
child_tail = helper(child_head)
30+
# Connect current node with the previous node
31+
prev.next = curr
32+
curr.prev = prev
3733

38-
# Splice the child list into the main list
39-
curr.next = child_head # Connect current node to child head
40-
child_head.prev = curr # Set child's prev to current
41-
curr.child = None # Nullify the child pointer (as required)
34+
# If there is a next node, push it onto the stack.
35+
# It will be processed after the child node (if any)
36+
if curr.next:
37+
stack.append(curr.next)
4238

43-
# If there was a next node after current, attach it to the end of child list
44-
if tempNext:
45-
child_tail.next = tempNext
46-
tempNext.prev = child_tail
39+
# If there is a child node, push it to the stack to be processed next
40+
# Set curr.child to None as it's flattened now
41+
if curr.child:
42+
stack.append(curr.child)
43+
curr.child = None # Important to remove child reference
4744

48-
# Update last to the end of the child list
49-
last = child_tail
45+
# Move prev forward for the next iteration
46+
prev = curr
5047

51-
# Move current to the saved next pointer to continue flattening
52-
curr = tempNext
53-
else:
54-
# No child, just move to next node
55-
last = curr
56-
curr = curr.next
48+
# Detach the pseudo head from the real head
49+
pseudoHead.next.prev = None
50+
return pseudoHead.next # Return the flattened list starting from the real head
5751

58-
return last # Return the tail of the fully flattened list
59-
60-
helper(head) # Start recursive flattening from head
61-
return head # Return head of flattened list

coding_2025/meta/lc_814.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
https://leetcode.com/problems/binary-tree-pruning/
3+
'''
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode:
7+
# def __init__(self, val=0, left=None, right=None):
8+
# self.val = val
9+
# self.left = left
10+
# self.right = right
11+
class Solution:
12+
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
13+
14+
def helper(node: TreeNode) -> bool:
15+
if not node:
16+
return False
17+
18+
# Check if any node in the left subtree contains a 1.
19+
left_contains_one = helper(node.left)
20+
21+
# Check if any node in the right subtree contains a 1.
22+
right_contains_one = helper(node.right)
23+
24+
# If the left subtree does not contain a 1, prune the subtree.
25+
if not left_contains_one:
26+
node.left = None
27+
28+
# If the right subtree does not contain a 1, prune the subtree.
29+
if not right_contains_one:
30+
node.right = None
31+
32+
return node.val or left_contains_one or right_contains_one
33+
34+
# Return the pruned tree if the tree contains a 1, otherwise return None.
35+
return root if helper(root) else None
36+

0 commit comments

Comments
 (0)