File tree Expand file tree Collapse file tree 3 files changed +107
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 3 files changed +107
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ def validTree (self , n : int , edges : List [List [int ]]) -> bool :
6+ """
7+ - Idea: ์ ํจํ ํธ๋ฆฌ๋ผ๋ฉด ๋ง์กฑํด์ผ ํ๋ ์กฐ๊ฑด๋ค์ ํ์ฉํ์ฌ ํธ๋ฆฌ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ค.
8+ - Time Complexity: O(v + e). v์ e๋ ๊ฐ๊ฐ ๋
ธ๋์ ์, ์ฐ๊ฒฐ๋ ์ (์ฃ์ง)์ ์
9+ ์ธ์ ๋ฆฌ์คํธ๋ก ๋ง๋ ๊ทธ๋ํ๋ฅผ ์ํํ ๋, ๋
ธ๋๋ง๋ค ์ฐ๊ฒฐ๋ ์ฃ์ง๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ํ์ํ๋ฏ๋ก O(v + e)์ด ์์๋๋ค.
10+ - Space Complexity: O(v + e). v์ e๋ ๊ฐ๊ฐ ๋
ธ๋์ ์, ์ฃ์ง์ ์
11+ ๊ทธ๋ํ๋ฅผ ์ธ์ ๋ฆฌ์คํธ๋ก ์ ์ฅํ๊ธฐ ์ํด O(v + e)์ ๊ณต๊ฐ์ด ํ์ํ๋ค.
12+
13+ """
14+ if len (edges ) != n - 1 :
15+ return False
16+
17+ graph = {i : [] for i in range (n )}
18+
19+ for v1 , v2 in edges :
20+ graph [v1 ].append (v2 )
21+ graph [v2 ].append (v1 )
22+
23+ visited = set ()
24+
25+ def DFS (v : int ) -> None :
26+ visited .add (v )
27+
28+ for adj in graph [v ]:
29+ if adj not in visited :
30+ DFS (adj )
31+
32+ DFS (0 )
33+
34+ return len (visited ) == n
Original file line number Diff line number Diff line change 1+ from typing import Optional
2+
3+
4+ # Definition for a binary tree node.
5+ class TreeNode :
6+ def __init__ (self , val = 0 , left = None , right = None ):
7+ self .val = val
8+ self .left = left
9+ self .right = right
10+
11+
12+ class Solution :
13+ def maxDepth (self , root : Optional [TreeNode ]) -> int :
14+ """
15+ - Idea: ์ด์ง ํธ๋ฆฌ์ ์ต๋ ๊น์ด๋ ํ์ฌ ๋
ธ๋์ ๊น์ด(1)์ ํ์ ํธ๋ฆฌ์ ์ต๋ ๊น์ด ์ค ํฐ ๊ฐ์ ๋ํ ๊ฒ์ผ๋ก ์ ์ํ๋ค.
16+ - Time Complexity: O(n). n์ ์ ์ฒด ๋
ธ๋์ ์
17+ ๋ชจ๋ ๋
ธ๋๋ฅผ ํ๋ฒ์ฉ ๋ฐฉ๋ฌธํ์ฌ ํ์ํ๊ธฐ ๋๋ฌธ์ O(n)์ด ์์๋๋ค.
18+ - Space Complexity: O(n). n์ ์ ์ฒด ๋
ธ๋์ ์
19+ ์ฌ๊ท์ ์ผ๋ก ํ์ํ ๋, ํธ์ถ ์คํ์ ์์ด๋ ํจ์ ํธ์ถ์ ์๋ ์ต๋ ํธ๋ฆฌ์ ๊น์ด์ ๊ฐ๋ค.
20+ ํธ๋ฆฌ๊ฐ ํธํฅ๋์ด ์๋ ์ต์
์ ๊ฒฝ์ฐ, ๊น์ด๊ฐ n์ด ๋ ์ ์๊ธฐ ๋๋ฌธ์ O(n)์ผ๋ก ํํํ ์ ์๋ค.
21+ """
22+ if root is None :
23+ return 0
24+
25+ left_depth = self .maxDepth (root .left )
26+ right_depth = self .maxDepth (root .right )
27+
28+ return max (left_depth , right_depth ) + 1
Original file line number Diff line number Diff line change 1+ from typing import Optional
2+
3+
4+ # Definition for singly-linked list.
5+ class ListNode :
6+ def __init__ (self , val = 0 , next = None ):
7+ self .val = val
8+ self .next = next
9+
10+
11+ class Solution :
12+ def reorderList (self , head : Optional [ListNode ]) -> None :
13+ """
14+ - Idea:
15+ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ๋ฐ์ผ๋ก ๋๋๊ณ , ๋ค์ชฝ(๋๋ฒ์งธ ๋ฆฌ์คํธ)์ ์ฐ๊ฒฐ ๋ฐฉํฅ์ ๋ฐ๋๋ก ๋ค์ง๋๋ค.
16+ ์ฒซ๋ฒ์งธ ๋ฆฌ์คํธ์ ๋๋ฒ์งธ ๋ฆฌ์คํธ์ ๋
ธ๋๋ฅผ ๋ฒ๊ฐ์๊ฐ๋ฉฐ ์ฐ๊ฒฐํ๋ค.
17+ - Time Complexity: O(n). n์ ์ ์ฒด ๋
ธ๋์ ์
18+ ๋ฆฌ์คํธ๋ฅผ ํ์ํ๊ณ ๋ณํฉํ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ๋น๋กํ๋ค.
19+ - Space Complexity: O(1)
20+ ๋ช ๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ ์์ ๊ณต๊ฐ๋ง ์ฐจ์งํ๋ค.
21+ """
22+ slow , fast = head , head
23+
24+ while fast and fast .next :
25+ slow = slow .next
26+ fast = fast .next .next
27+
28+ mid = slow .next
29+ slow .next = None
30+
31+ prev , cur = None , mid
32+
33+ while cur :
34+ next_temp = cur .next
35+ cur .next = prev
36+ prev = cur
37+ cur = next_temp
38+
39+ first , second = head , prev
40+
41+ while second :
42+ first_next , second_next = first .next , second .next
43+ first .next = second
44+ second .next = first_next
45+ first , second = first_next , second_next
You canโt perform that action at this time.
0 commit comments