Skip to content

Commit 7f52ecd

Browse files
authored
Merge pull request #1828 from wozlsla/main
[wozlsla] WEEK 04 solutions
2 parents c56b100 + b601b5d commit 7f52ecd

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
# Intuition
3+
트리 전체 순회
4+
5+
# Approach
6+
-
7+
8+
# Complexity
9+
time : O(N)
10+
space : O(N)
11+
"""
12+
13+
# Definition for a binary tree node.
14+
# class TreeNode:
15+
# def __init__(self, val=0, left=None, right=None):
16+
# self.val = val
17+
# self.left = left
18+
# self.right = right
19+
20+
21+
# Stack (top-down)
22+
class Solution:
23+
def maxDepth(self, root: Optional[TreeNode]) -> int:
24+
25+
# edge case
26+
if not root:
27+
return 0
28+
29+
max_depth = 0
30+
stack = [(root, 1)]
31+
32+
while stack:
33+
node, depth = stack.pop()
34+
35+
max_depth = max(depth, max_depth) # update depth
36+
37+
if node.left:
38+
stack.append((node.left, depth + 1))
39+
if node.right:
40+
stack.append((node.right, depth + 1))
41+
42+
return max_depth
43+
44+
45+
""" 재귀 (bottom-top)
46+
# max_depth : 현재 노드를 기준으로, 좌측과 우측 자식 트리 중 max_depth가 큰 값을 선택해 + 1
47+
48+
class Solution:
49+
def maxDepth(self, root: Optional[TreeNode]) -> int:
50+
51+
# 기저조건
52+
if not root:
53+
return 0
54+
55+
return 1 + max(
56+
self.maxDepth(root.left),
57+
self.maxDepth(root.right),
58+
)
59+
"""

merge-two-sorted-lists/wozlsla.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
# Intuition
3+
-
4+
# Approach
5+
-
6+
# Complexity
7+
time : O(N+M)
8+
space : O(1) / O(N+M)
9+
"""
10+
11+
from typing import Optional
12+
13+
14+
# Definition for singly-linked list.
15+
class ListNode:
16+
def __init__(self, val=0, next=None):
17+
self.val = val
18+
self.next = next
19+
20+
21+
class Solution:
22+
def mergeTwoLists(
23+
self, list1: Optional[ListNode], list2: Optional[ListNode]
24+
) -> Optional[ListNode]:
25+
26+
dummy = ListNode(None) # -> None(고정)
27+
node = dummy # -> None(이동)
28+
29+
while list1 and list2:
30+
31+
if list1.val < list2.val:
32+
node.next = list1
33+
list1 = list1.next
34+
else:
35+
node.next = list2
36+
list2 = list2.next
37+
38+
node = node.next # (이동)
39+
40+
# 반복문 종료
41+
node.next = list1 or list2
42+
43+
return dummy.next # 병합 리스트의 첫 번째 노드를 가리킴
44+
45+
46+
# list1: 1 -> 2 -> 4
47+
list1_node3 = ListNode(4)
48+
list1_node2 = ListNode(2, list1_node3)
49+
list1_node1 = ListNode(1, list1_node2)
50+
51+
# list2: 1 -> 3 -> 4
52+
list2_node3 = ListNode(4)
53+
list2_node2 = ListNode(3, list2_node3)
54+
list2_node1 = ListNode(1, list2_node2)
55+
56+
sol = Solution()
57+
merged_list = sol.mergeTwoLists(list1_node1, list2_node1)
58+
59+
# 결과 출력
60+
current = merged_list
61+
result = []
62+
while current:
63+
result.append(current.val)
64+
current = current.next
65+
66+
print(result)
67+
68+
69+
""" 재귀
70+
class Solution:
71+
def mergeTwoLists(
72+
self, list1: Optional[ListNode], list2: Optional[ListNode]
73+
) -> Optional[ListNode]:
74+
75+
# 기저조건
76+
if not (list1 and list2):
77+
return list1 or list2
78+
79+
if list1.val < list2.val:
80+
list1.next = self.mergeTwoLists(list1.next, list2)
81+
return list1
82+
else:
83+
list2.next = self.mergeTwoLists(list1, list2.next)
84+
return list2
85+
"""

0 commit comments

Comments
 (0)