Skip to content

Commit fddf115

Browse files
committed
[김병현, bhyun-kim] Week2 Solution
1 parent bfd7843 commit fddf115

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

invert-binary-tree/bhyun-kim.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
226. Invert Binary Tree
3+
https://leetcode.com/problems/invert-binary-tree/description/
4+
5+
Solution
6+
Recursively swaps the left and right children of the root node.
7+
8+
1. Check if the root has left and right children.
9+
2. If it does, invert the left and right children.
10+
3. If it doesn't, invert the left or right child.
11+
4. Return the root.
12+
13+
Time complexity: O(n)
14+
Space complexity: O(n)
15+
16+
"""
17+
from typing import Optional
18+
19+
20+
# Definition for a binary tree node.
21+
class TreeNode:
22+
def __init__(self, val=0, left=None, right=None):
23+
self.val = val
24+
self.left = left
25+
self.right = right
26+
27+
28+
class Solution:
29+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
30+
has_left = hasattr(root, "left")
31+
has_right = hasattr(root, "right")
32+
33+
if has_left and has_right:
34+
root.left = self.invertTree(root.left)
35+
root.right = self.invertTree(root.right)
36+
root.left, root.right = root.right, root.left
37+
elif has_left:
38+
root.left = self.invertTree(root.left)
39+
root.left, root.right = None, root.left
40+
elif has_right:
41+
root.right = self.invertTree(root.right)
42+
root.left, root.right = root.right, None
43+
44+
return root
45+
46+
47+
def main():
48+
test_cases = [
49+
[
50+
TreeNode(
51+
4,
52+
TreeNode(2, TreeNode(1), TreeNode(3)),
53+
TreeNode(7, TreeNode(6), TreeNode(9)),
54+
),
55+
TreeNode(
56+
4,
57+
TreeNode(7, TreeNode(9), TreeNode(6)),
58+
TreeNode(2, TreeNode(3), TreeNode(1)),
59+
),
60+
],
61+
[
62+
TreeNode(2, TreeNode(1), TreeNode(3)),
63+
TreeNode(2, TreeNode(3), TreeNode(1)),
64+
],
65+
[
66+
TreeNode(),
67+
TreeNode(),
68+
],
69+
]
70+
s = Solution()
71+
72+
for test_case in test_cases:
73+
root_input, expected = test_case
74+
assert s.invertTree(root_input) == expected
75+
76+
77+
if __name__ == "__main__":
78+
main()

linked-list-cycle/bhyun-kim.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
141. Linked List Cycle
3+
https://leetcode.com/problems/linked-list-cycle/description/
4+
5+
Solution
6+
Floyd's Tortoise and Hare algorithm:
7+
8+
1. Initialize two pointers, slower and faster, to the head of the linked list.
9+
faster is one step ahead of slower.
10+
2. Move the slower pointer by one and the faster pointer by two.
11+
3. If the slower and faster pointers meet, return True.
12+
4. If the faster pointer reaches the end of the linked list, return False.
13+
14+
Time complexity: O(n)
15+
Space complexity: O(1)
16+
"""
17+
18+
19+
from typing import Optional
20+
21+
22+
# Definition for singly-linked list.
23+
class ListNode:
24+
def __init__(self, x):
25+
self.val = x
26+
self.next = None
27+
28+
29+
class Solution:
30+
def hasCycle(self, head: Optional[ListNode]) -> bool:
31+
if not hasattr(head, "next"):
32+
return False
33+
34+
slower = head
35+
faster = head.next
36+
37+
while slower != faster:
38+
if not hasattr(faster, "next"):
39+
return False
40+
elif not hasattr(faster.next, "next"):
41+
return False
42+
slower = slower.next
43+
faster = faster.next.next
44+
45+
return True

merge-two-sorted-lists/bhyun-kim.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
21. Merge Two Sorted Lists
3+
https://leetcode.com/problems/merge-two-sorted-lists/
4+
5+
Solution
6+
Recursively merges two sorted linked lists.
7+
8+
1. Check if either list is empty.
9+
2. Compare the values of the two lists.
10+
3. Return a new node with the smaller value and the merged list.
11+
12+
Time complexity: O(n)
13+
Space complexity: O(n)
14+
"""
15+
from typing import Optional
16+
17+
18+
# Definition for singly-linked list.
19+
class ListNode:
20+
def __init__(self, val=0, next=None):
21+
self.val = val
22+
self.next = next
23+
24+
25+
class Solution:
26+
def mergeTwoLists(
27+
self, list1: Optional[ListNode], list2: Optional[ListNode]
28+
) -> Optional[ListNode]:
29+
if list1 is None:
30+
return list2
31+
32+
if list2 is None:
33+
return list1
34+
35+
if list1.val < list2.val:
36+
val = list1.val
37+
list1 = list1.next
38+
else:
39+
val = list2.val
40+
list2 = list2.next
41+
42+
return ListNode(val=val, next=self.mergeTwoLists(list1, list2))

reverse-linked-list/bhyun-kim.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
206. Reverse Linked List
3+
https://leetcode.com/problems/reverse-linked-list/description/
4+
5+
Solution
6+
Iteratively reverses a singly linked list.
7+
8+
1. Initialize two pointers, prev and curr, to None and the head of the linked list, respectively.
9+
2. While curr is not None:
10+
a. Save the next node of curr.
11+
b. Set the next node of curr to prev.
12+
c. Move prev and curr to the next nodes.
13+
14+
Time complexity: O(n)
15+
Space complexity: O(1)
16+
17+
"""
18+
19+
20+
from typing import Optional
21+
22+
23+
# Definition for singly-linked list.
24+
class ListNode:
25+
def __init__(self, val=0, next=None):
26+
self.val = val
27+
self.next = next
28+
29+
30+
class Solution:
31+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
32+
prev, curr = None, head
33+
34+
while curr:
35+
next_temp = curr.next
36+
curr.next = prev
37+
prev = curr
38+
curr = next_temp
39+
40+
return prev

0 commit comments

Comments
 (0)