-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) |
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) |
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 | ||
|
||
while right.next: | ||
left = left.next | ||
right = right.next | ||
|
||
left.next = left.next.next ## deleting node | ||
|
||
return head | ||
|
||
# TC: O(n), SC: O(1) |
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. helper ν¨μλ₯Ό νμ©νμ¬ λͺ¨λνλ₯Ό νμ μ , λ무 μ’μ κ² κ°μ΅λλ€! |
||
|
||
return helper(root, -inf, inf) | ||
|
||
## TC: O(n), SC: O(n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSμ λ€λ₯΄κ² μμ£Ό κ°λ¨ν not μ°μ°μλ§μΌλ‘ 쑰건μ νμΈνκ³ λ°νν μ μλ€λ μ μ΄ μ’λ€μ!