-
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.
- Loading branch information
Showing
3 changed files
with
167 additions
and
68 deletions.
There are no files selected for viewing
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,83 @@ | ||
# Intuition | ||
<!-- Describe your first thoughts on how to solve this problem. --> | ||
νΈλ¦¬λ₯Ό μννλ λ°©λ²λ€ μ€, λ 벨 μμλλ‘ μννλ λ°©μμ μκ³ μμ΄ μ΄λ₯Ό μ΄μ©νλ€. | ||
# Approach | ||
1. BFSλ₯Ό μ§ννλ€. | ||
2. μ§ννκΈ° μ , ν΄λΉ λ 벨μ λͺκ°μ μμκ° μλμ§(`currLen`)μ μ μ₯νλ€. | ||
3. μ μ₯λ μμλ§νΌ μννλ€λ©΄, λ 벨μ λ€ λλ¬λ³Έ κ²μ΄λ―λ‘ λΆκ°μμ (`levels = append(levels, level)`)μ νλ€. | ||
|
||
# Complexity | ||
- Time complexity: $O(n)$ | ||
- νΈλ¦¬μ μμλ₯Ό nκ°λΌκ³ νμ λ, λͺ¨λ μμλ₯Ό μννλ λΉμ© `O(n)`μ΄ μλͺ¨λλ€. | ||
- Space complexity: $O(n)$ | ||
- νΈλ¦¬μ μμλ₯Ό nκ°λΌκ³ νμ λ, λͺ¨λ μμλ€μ μ μ₯νλ λ°°μ΄μ΄ `O(n)`μ μλͺ¨νλ€. | ||
|
||
# Code | ||
```go | ||
func levelOrder(root *TreeNode) [][]int { | ||
levels := make([][]int, 0) | ||
|
||
if root == nil { | ||
return levels | ||
} | ||
|
||
q := []*TreeNode{root} | ||
|
||
for len(q) > 0 { | ||
level := make([]int, 0) | ||
currLen := len(q) | ||
for i := 0; i < currLen; i++ { | ||
front := q[0] | ||
level = append(level, front.Val) | ||
q = q[1:] | ||
|
||
if front.Left != nil { | ||
q = append(q, front.Left) | ||
} | ||
if front.Right != nil { | ||
q = append(q, front.Right) | ||
} | ||
} | ||
levels = append(levels, level) | ||
} | ||
|
||
return levels | ||
} | ||
|
||
``` | ||
|
||
```go | ||
func levelOrder(root *TreeNode) [][]int { | ||
levels := make([][]int, 0) | ||
|
||
if root == nil { | ||
return levels | ||
} | ||
|
||
q := []*TreeNode{root} | ||
var nextQ []*TreeNode | ||
|
||
for len(q) > 0 { | ||
level := make([]int, 0) | ||
for _, front := range q { | ||
level = append(level, front.Val) | ||
|
||
if front.Left != nil { | ||
nextQ = append(nextQ, front.Left) | ||
} | ||
if front.Right != nil { | ||
nextQ = append(nextQ, front.Right) | ||
} | ||
} | ||
q, nextQ = nextQ, q[:0] | ||
|
||
levels = append(levels, level) | ||
} | ||
|
||
return levels | ||
} | ||
|
||
``` | ||
- 첫 λ²μ§Έ μ½λλ `q[1:]`μ μ΄μ©ν΄μ νμ `pop()`μ ꡬννλ€. νμ§λ§ GoLangμμλ `pop()`μ νλ€κ³ ν΄μ, μ°Έμ‘°κ° ν΄μ λμ§ μμ λ©λͺ¨λ¦¬λ₯Ό κ³μ μ‘μλ¨Ήλλ€. | ||
- `pop()`μ νλλΌλ `q`κ° μννλ λͺ¨λ μμλ€μ μ°Έμ‘°νκ³ μλ€. | ||
- λ λ²μ§Έ μ½λλ `q, nextQ = nextQ, q[:0]`μ μ΄μ©ν΄μ `q`μ `nextQ`μ λ©λͺ¨λ¦¬ μμμ κ΅μ²΄ν λΏ, λΆκ°μ μΈ λ©λͺ¨λ¦¬ 곡κ°μ νμλ‘ νμ§ μμ λμ± ν¨μ¨μ μ΄λ€. |
This file was deleted.
Oops, something went wrong.
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,84 @@ | ||
# Intuition (Array) | ||
BSTλ μ λ ¬ κ΄κ³λ₯Ό λ§μ‘±νλ―λ‘, μμμ λ§κ² λ°°μ΄μ μ μ₯ν ν μ λ ¬ κ΄κ³κ° λ§λμ§ λΉκ΅νλ€. | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
1. μ€μ μν(μ’ --> λ£¨νΈ --> μ°)λ₯Ό νλ©° λ°°μ΄μ μ±μ΄λ€. (`fillNodes()`) | ||
2. λ°°μ΄μ μννλ©° μ λ ¬ κ΄κ³κ° λ²μ΄λ¬λμ§ νλ³νλ€. (`if nodes[i] >= nodes[i+1]`) | ||
|
||
# Complexity | ||
- Time complexity: $O(n)$ | ||
- νΈλ¦¬μ μμλ₯Ό nκ°λΌκ³ νμ λ, λͺ¨λ μμλ₯Ό μννλ λΉμ© `O(n)`μ΄ μλͺ¨λλ€. | ||
- Space complexity: $O(n)$ | ||
- νΈλ¦¬μ μμλ₯Ό nκ°λΌκ³ νμ λ, λͺ¨λ μμλ€μ μ μ₯νλ λ°°μ΄μ΄ `O(n)`μ μλͺ¨νλ€. | ||
|
||
# Code | ||
```go | ||
func fillNodes(root *TreeNode) []int { | ||
nodes := make([]int, 0) | ||
if root.Left != nil { | ||
nodes = append(nodes, fillNodes(root.Left)...) | ||
} | ||
nodes = append(nodes, root.Val) | ||
if root.Right != nil { | ||
nodes = append(nodes, fillNodes(root.Right)...) | ||
} | ||
return nodes | ||
} | ||
|
||
func isValidBST(root *TreeNode) bool { | ||
nodes := fillNodes(root) | ||
for i := 0; i < len(nodes)-1; i++ { | ||
if nodes[i] >= nodes[i+1] { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
``` | ||
# Intuition (Recursion) | ||
μμ λ₯Ό μ°Έκ³ νμ λ, BSTμ λ²μ(μ΅μ, μ΅λ)λ₯Ό λ²μ΄λμ§ μλμ§λ₯Ό μ μ§νλ©΄ νλ³ν μ μλ€κ³ μκ°νλ€. | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
1. μ μ μν(λ£¨νΈ --> μ’ --> μ°)λ₯Ό νλ€. | ||
2. ν΄λΉ λ Έλμμ μ£Όμ΄μ§ λ²μλ₯Ό λ²μ΄λλμ§ νλ³νλ€. (`if !(min < root.Val && root.Val < max)`) | ||
3. μμ λ Έλλ€μ λν΄μλ BSTκ° λ§μ‘±νλμ§ μ¬κ·ν¨μ νΈμΆμ ν΅ν΄ νλ³νλ€. | ||
- κ°λ₯ν λ²μλ ν΄λΉ λ£¨νΈ λ Έλλ₯Ό κΈ°μ€μΌλ‘ κ°±μ νλ€. | ||
# Complexity | ||
- Time complexity: $O(n)$ | ||
- νΈλ¦¬μ μμλ₯Ό nκ°λΌκ³ νμ λ, λͺ¨λ μμλ₯Ό μννλ λΉμ© `O(n)`μ΄ μλͺ¨λλ€. | ||
- Space complexity: $O(n)$ | ||
- νΈλ¦¬μ μμλ₯Ό nκ°λΌκ³ νμ λ, νΈλ¦¬μ λμ΄λ§νΌ μ½ μ€νμ΄ λ°μνμ¬ λ³΅μ‘λκ° μλͺ¨λλ€. | ||
- μ΅μ μ κ²½μ°(`skewed tree`), λμ΄λ `n`κ°μ΄λ―λ‘ `O(n)`μ μλͺ¨νλ€. | ||
- μ΅μ μ κ²½μ°(`perfect binary tree`), λμ΄λ `log(n)`κ° μ΄λ―λ‘ `O(log(n))`μ μλͺ¨νλ€. | ||
|
||
# Code | ||
```go | ||
func isValidBSTFrom(root *TreeNode, min, max int) bool { | ||
if root == nil { | ||
return true | ||
} | ||
if !(min < root.Val && root.Val < max) { | ||
return false | ||
} | ||
return isValidBSTFrom(root.Left, min, root.Val) && isValidBSTFrom(root.Right, root.Val, max) | ||
} | ||
|
||
func isValidBST(root *TreeNode) bool { | ||
return isValidBSTFrom(root, math.MinInt, math.MaxInt) | ||
} | ||
|
||
``` | ||
|
||
# Learned | ||
- `math` λΌμ΄λΈλ¬λ¦¬μ `MinInt`, `MaxInt` μ¬μ©λ² | ||
- `MinInt`λ `-1 << 31`λ‘ κ³μ°νλ€. | ||
- μ `-`μ μ¬ννΈνλμ§ κΆκΈν΄μ μ°Ύμλ΄€λ€. | ||
- λΉνΈ 맨λ€κ° 1μΈ μλ€μ `<< 31`μ νλ©΄ λͺ¨λ `MinInt`λ₯Ό λ§λ€ μ μμ§λ§, `1 << 31`λ‘ `MaxInt`μ λλΉλλλ‘ `-1`μ μ νν κ² κ°λ€. λ€λ₯Έ μλ€μ λΉν΄ μ’ λ μ§κ΄μ μ΄κΈ°λ νλ€. | ||
|
||
- `...` operator (Three dots, Ellipsis) | ||
- unpackingμ νλ μ©λμ΄λ€. | ||
- μ¬λ‘ | ||
- ν¨μ μΈμλ‘ λͺ κ°μΈμ§ μ μ μλ μΈμλ€μ΄ λ€μ΄μ¬ λ | ||
- λ°°μ΄ μ΄κΈ°ν μ κΈΈμ΄λ₯Ό λͺ°λΌ μ»΄νμΌλ¬μκ² λ§‘κΈ°κ³ μΆμ λ | ||
- μ°Έκ³ : [λ§ν¬](https://blog.advenoh.pe.kr/go/Go%EC%97%90%EC%84%9C-%EC%82%BC-%EB%8F%84%ED%8A%B8-dot-%EC%82%AC%EC%9A%A9%EB%B0%A9%EB%B2%95-Three-Dots-Usage/) |