-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solve: lowest-common-ancestor-of-a-binary-search-tree
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
lowest-common-ancestor-of-a-binary-search-tree/invidam.go.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Intuition | ||
μ΄μ μ νμ΄λ΄€λ λ¬Έμ μλ€! μ΄λ €μ΄ λ¬Έμ μλ€λ μκ°μ λ²μ΄λμ§κ³ μ΅λν κ°λ¨νκ² νμ΄λ³΄λ €κ³ νλ€. | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
1. λ λ Έλ(`p`, `q`) λͺ¨λ μ΄λ ν κ²½λ‘λ₯Ό κ±°μ³, ν΄λΉ λ ΈλκΉμ§ λμ°©νλμ§ μ‘°μλ€μ λ°°μ΄(`routes`)μ μ μ₯νλ€. | ||
2. λ°°μ΄μ νλμ© λΉκ΅ν΄κ°λ©°, λ λ Έλκ° μκ°λ¦¬λ μ§μ (`pRoutes[idx] != qRoutes[idx]`)μ μ°Ύλλ€. | ||
3. κ·Έ μ§μ λ°λ‘ μ΄μ μ§μ (`[idx-1]`)μ΄ μ΅μ κ³΅ν΅ μ‘°μμ΄λ€. | ||
# Complexity | ||
- Time complexity | ||
- νκ· : $O(log(n))$ | ||
- μ΅μ : $O(n)$ | ||
- νΈλ¦¬μ λμ΄λ§νΌ μνλ₯Ό νκ²λλ€. λ Έλκ° nκ° μ΄λ―λ‘, νΈλ¦¬μ λμ΄λ μ΅μ `log(n)`, μ΅μ `n`μ΄ λλ€. | ||
|
||
- Space complexity | ||
- νκ· : $O(log(n))$ | ||
- μ΅μ : $O(n)$ | ||
- νΈλ¦¬μ λμ΄λ§νΌ μνλ₯Ό νκ²λλ€. λ Έλκ° nκ° μ΄λ―λ‘, νΈλ¦¬μ λμ΄λ μ΅μ `log(n)`, μ΅μ `n`μ΄ λλ€. | ||
|
||
# Code | ||
```go | ||
func getRoutes(head, target *TreeNode) []*TreeNode { | ||
routes := make([]*TreeNode, 0) | ||
|
||
curr := head | ||
for curr.Val != target.Val { | ||
routes = append(routes, curr) | ||
if target.Val == curr.Val { | ||
break | ||
} else if target.Val < curr.Val { | ||
curr = curr.Left | ||
} else { | ||
curr = curr.Right | ||
} | ||
} | ||
return append(routes, curr) | ||
} | ||
|
||
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { | ||
pRoutes := getRoutes(root, p) | ||
qRoutes := getRoutes(root, q) | ||
|
||
idx := 0 | ||
for idx < min(len(pRoutes), len(qRoutes)) && pRoutes[idx] == qRoutes[idx] { | ||
idx++ | ||
} | ||
|
||
return pRoutes[idx-1] | ||
} | ||
|
||
``` | ||
# Intuition & Approach | ||
(μ루μ μ ν΄κ²°λ² μ°Έκ³ ) | ||
|
||
λ λ Έλλ 곡ν΅μ‘°μκΉμ§λ λμΌν λμκ΄κ³λ₯Ό κ°μ§κ³ μλ€κ°, κ³΅ν΅ μ‘°μ μ΄νλ‘ λμκ΄κ³κ° ꡬλΆλλ€. | ||
λ°λΌμ, 루νΈμμ λμΌν λμκ΄κ³κ° μλ μ‘°μκΉμ§ μ΄λνλ€. (λ€μ λ§ν΄, λμ κ΄κ³κ° ꡬλΆλλ νΉμ μ§μ μ΄ λ°μνλ€λ©΄ κ·Έ μ§μ μ λΆλͺ¨κ° κ³΅ν΅ μ‘°μμ΄λ€.) | ||
# Complexity | ||
- Time complexity | ||
- νκ· : $O(log(n))$ | ||
- μ΅μ : $O(n)$ | ||
- νΈλ¦¬μ λμ΄λ§νΌ μνλ₯Ό νκ²λλ€. λ Έλκ° nκ° μ΄λ―λ‘, νΈλ¦¬μ λμ΄λ μ΅μ `log(n)`, μ΅μ `n`μ΄ λλ€. | ||
|
||
- Space complexity: $O(1)$ | ||
- λ³λ μλ£κ΅¬μ‘°λ₯Ό μ¬μ©νμ§ μκ³ , λ§ν¬λ 리μ€νΈμ μνλ§μ΄ μ‘΄μ¬νλ€. | ||
|
||
# Code | ||
## For-loop | ||
```go | ||
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { | ||
curr := root | ||
for { | ||
if p.Val < curr.Val && q.Val < curr.Val { | ||
curr = curr.Left | ||
} else if p.Val > curr.Val && q.Val > curr.Val { | ||
curr = curr.Right | ||
} else { | ||
break | ||
} | ||
} | ||
return curr | ||
} | ||
|
||
``` | ||
## Recursion | ||
```go | ||
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { | ||
if p.Val < root.Val && q.Val < root.Val { | ||
return lowestCommonAncestor(root.Left, p, q) | ||
} else if p.Val > root.Val && q.Val > root.Val { | ||
return lowestCommonAncestor(root.Right, p, q) | ||
} | ||
return root | ||
} | ||
|
||
``` | ||
: ν¨μ μ½μ€νμ΄ νΈλ¦¬μ λμ΄λ§νΌ μ¦κ°νλ―λ‘, κ³΅κ° λ³΅μ‘λκ° O(n)κΉμ§ μ¦κ°ν μ μλ€. (μ²μ ν΄κ²°λ²μ κ³΅κ° λ³΅μ‘λμ²λΌ.) |