Skip to content

Commit e976c4c

Browse files
authored
Merge pull request #2481 from YOOHYOJEONG/main
[YOOHYOJEONG] WEEK 04 solutions
2 parents 774d101 + ec51c39 commit e976c4c

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# https://leetcode.com/problems/maximum-depth-of-binary-tree
2+
3+
class Solution:
4+
def maxDepth(self, root):
5+
# base case:
6+
# ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด (๋นˆ ํŠธ๋ฆฌ ๋˜๋Š” leaf์˜ ์ž์‹)
7+
# ๊นŠ์ด๋Š” 0
8+
if not root:
9+
return 0
10+
11+
# ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ณ„์‚ฐ
12+
left_depth = self.maxDepth(root.left)
13+
14+
# ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ณ„์‚ฐ
15+
right_depth = self.maxDepth(root.right)
16+
17+
# ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•ด์•ผ ํ•˜๋ฏ€๋กœ +1
18+
# ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์ค‘ ๋” ๊นŠ์€ ์ชฝ์„ ์„ ํƒ
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# https://leetcode.com/problems/merge-two-sorted-lists
2+
3+
# gpt์˜ ๋„์›€์„ ๋ฐ›์•„ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค
4+
class Solution:
5+
def mergeTwoLists(self, list1, list2):
6+
# base case 1:
7+
# list1์ด ๋น„์–ด์žˆ์œผ๋ฉด, ๋‚จ์€ list2๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
8+
if not list1:
9+
return list2
10+
11+
# base case 2:
12+
# list2๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด, ๋‚จ์€ list1์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
13+
if not list2:
14+
return list1
15+
16+
# ๋‘ ๋ฆฌ์ŠคํŠธ์˜ ํ˜„์žฌ ๋…ธ๋“œ ๊ฐ’์„ ๋น„๊ต
17+
if list1.val < list2.val:
18+
# list1์˜ ๊ฐ’์ด ๋” ์ž‘์œผ๋ฉด
19+
# list1์„ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ์„ ํƒ
20+
21+
# list1์˜ ๋‹ค์Œ ๋…ธ๋“œ๋Š”
22+
# (list1.next์™€ list2๋ฅผ ๋‹ค์‹œ ๋ณ‘ํ•ฉํ•œ ๊ฒฐ๊ณผ)๋กœ ์—ฐ๊ฒฐ
23+
list1.next = self.mergeTwoLists(list1.next, list2)
24+
25+
# ํ˜„์žฌ ์„ ํƒ๋œ list1์„ ๋ฐ˜ํ™˜
26+
return list1
27+
else:
28+
# list2์˜ ๊ฐ’์ด ๋” ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด
29+
# list2๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ์„ ํƒ
30+
31+
# list2์˜ ๋‹ค์Œ ๋…ธ๋“œ๋Š”
32+
# (list1๊ณผ list2.next๋ฅผ ๋‹ค์‹œ ๋ณ‘ํ•ฉํ•œ ๊ฒฐ๊ณผ)๋กœ ์—ฐ๊ฒฐ
33+
list2.next = self.mergeTwoLists(list1, list2.next)
34+
35+
# ํ˜„์žฌ ์„ ํƒ๋œ list2๋ฅผ ๋ฐ˜ํ™˜
36+
return list2

0 commit comments

Comments
ย (0)