Skip to content

Commit

Permalink
solve: lowest-common-ancestor-of-a-binary-search-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Invidam committed Jun 12, 2024
1 parent eede72a commit 22edc6a
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/invidam.go.md
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)κΉŒμ§€ 증가할 수 μžˆλ‹€. (처음 ν•΄κ²°λ²•μ˜ 곡간 λ³΅μž‘λ„μ²˜λŸΌ.)

0 comments on commit 22edc6a

Please sign in to comment.