Skip to content

[Leo] 7th Week solutions #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions binary-tree-level-order-traversal/Leo.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/Leo.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions remove-nth-node-from-end-of-list/Leo.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS와 λ‹€λ₯΄κ²Œ μ•„μ£Ό κ°„λ‹¨νžˆ not μ—°μ‚°μžλ§ŒμœΌλ‘œ 쑰건을 ν™•μΈν•˜κ³  λ°˜ν™˜ν•  수 μžˆλ‹€λŠ” 점이 μ’‹λ„€μš”!


while right.next:
left = left.next
right = right.next

left.next = left.next.next ## deleting node

return head

# TC: O(n), SC: O(1)
23 changes: 23 additions & 0 deletions reorder-list/Leo.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions validate-binary-search-tree/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript 처럼, λ°˜ν™˜ νƒ€μž…μ„ μ§€μ •ν•  수 μžˆλŠ” μ½”λ“œλ“€μ΄ ν₯λ―Έλ‘œμ› μŠ΅λ‹ˆλ‹€.
ν™•μ‹€νžˆ νƒ€μž…μ„ μ§€μ •ν•˜λ‹ˆ 가독성도 μ˜¬λΌκ°€κ³  훨씬 μ’‹λ„€μš”!

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)
Comment on lines +3 to +8
Copy link
Contributor

@yolophg yolophg Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper ν•¨μˆ˜λ₯Ό ν™œμš©ν•˜μ—¬ λͺ¨λ“ˆν™”λ₯Ό ν•˜μ‹  점, λ„ˆλ¬΄ 쒋은 것 κ°™μŠ΅λ‹ˆλ‹€!


return helper(root, -inf, inf)

## TC: O(n), SC: O(n)