File tree Expand file tree Collapse file tree
maximum-depth-of-binary-tree Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ # ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ์ค ๋ ๊น์ ์ชฝ์ ์ ํ
Original file line number Diff line number Diff line change 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
You canโt perform that action at this time.
0 commit comments