|
| 1 | +# Intuition (Array) |
| 2 | +BST๋ ์ ๋ ฌ ๊ด๊ณ๋ฅผ ๋ง์กฑํ๋ฏ๋ก, ์์์ ๋ง๊ฒ ๋ฐฐ์ด์ ์ ์ฅํ ํ ์ ๋ ฌ ๊ด๊ณ๊ฐ ๋ง๋์ง ๋น๊ตํ๋ค. |
| 3 | +# Approach |
| 4 | +<!-- Describe your approach to solving the problem. --> |
| 5 | +1. ์ค์ ์ํ(์ข --> ๋ฃจํธ --> ์ฐ)๋ฅผ ํ๋ฉฐ ๋ฐฐ์ด์ ์ฑ์ด๋ค. (`fillNodes()`) |
| 6 | +2. ๋ฐฐ์ด์ ์ํํ๋ฉฐ ์ ๋ ฌ ๊ด๊ณ๊ฐ ๋ฒ์ด๋ฌ๋์ง ํ๋ณํ๋ค. (`if nodes[i] >= nodes[i+1]`) |
| 7 | + |
| 8 | +# Complexity |
| 9 | +- Time complexity: $O(n)$ |
| 10 | + - ํธ๋ฆฌ์ ์์๋ฅผ n๊ฐ๋ผ๊ณ ํ์ ๋, ๋ชจ๋ ์์๋ฅผ ์ํํ๋ ๋น์ฉ `O(n)`์ด ์๋ชจ๋๋ค. |
| 11 | +- Space complexity: $O(n)$ |
| 12 | + - ํธ๋ฆฌ์ ์์๋ฅผ n๊ฐ๋ผ๊ณ ํ์ ๋, ๋ชจ๋ ์์๋ค์ ์ ์ฅํ๋ ๋ฐฐ์ด์ด `O(n)`์ ์๋ชจํ๋ค. |
| 13 | + |
| 14 | +# Code |
| 15 | +```go |
| 16 | +func fillNodes(root *TreeNode) []int { |
| 17 | + nodes := make([]int, 0) |
| 18 | + if root.Left != nil { |
| 19 | + nodes = append(nodes, fillNodes(root.Left)...) |
| 20 | + } |
| 21 | + nodes = append(nodes, root.Val) |
| 22 | + if root.Right != nil { |
| 23 | + nodes = append(nodes, fillNodes(root.Right)...) |
| 24 | + } |
| 25 | + return nodes |
| 26 | +} |
| 27 | + |
| 28 | +func isValidBST(root *TreeNode) bool { |
| 29 | + nodes := fillNodes(root) |
| 30 | + for i := 0; i < len(nodes)-1; i++ { |
| 31 | + if nodes[i] >= nodes[i+1] { |
| 32 | + return false |
| 33 | + } |
| 34 | + } |
| 35 | + return true |
| 36 | +} |
| 37 | + |
| 38 | +``` |
| 39 | +# Intuition (Recursion) |
| 40 | +์์ ๋ฅผ ์ฐธ๊ณ ํ์ ๋, BST์ ๋ฒ์(์ต์, ์ต๋)๋ฅผ ๋ฒ์ด๋์ง ์๋์ง๋ฅผ ์ ์งํ๋ฉด ํ๋ณํ ์ ์๋ค๊ณ ์๊ฐํ๋ค. |
| 41 | +# Approach |
| 42 | +<!-- Describe your approach to solving the problem. --> |
| 43 | +1. ์ ์ ์ํ(๋ฃจํธ --> ์ข --> ์ฐ)๋ฅผ ํ๋ค. |
| 44 | +2. ํด๋น ๋
ธ๋์์ ์ฃผ์ด์ง ๋ฒ์๋ฅผ ๋ฒ์ด๋๋์ง ํ๋ณํ๋ค. (`if !(min < root.Val && root.Val < max)`) |
| 45 | +3. ์์ ๋
ธ๋๋ค์ ๋ํด์๋ BST๊ฐ ๋ง์กฑํ๋์ง ์ฌ๊ทํจ์ ํธ์ถ์ ํตํด ํ๋ณํ๋ค. |
| 46 | + - ๊ฐ๋ฅํ ๋ฒ์๋ ํด๋น ๋ฃจํธ ๋
ธ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ฐฑ์ ํ๋ค. |
| 47 | +# Complexity |
| 48 | +- Time complexity: $O(n)$ |
| 49 | + - ํธ๋ฆฌ์ ์์๋ฅผ n๊ฐ๋ผ๊ณ ํ์ ๋, ๋ชจ๋ ์์๋ฅผ ์ํํ๋ ๋น์ฉ `O(n)`์ด ์๋ชจ๋๋ค. |
| 50 | +- Space complexity: $O(n)$ |
| 51 | + - ํธ๋ฆฌ์ ์์๋ฅผ n๊ฐ๋ผ๊ณ ํ์ ๋, ํธ๋ฆฌ์ ๋์ด๋งํผ ์ฝ ์คํ์ด ๋ฐ์ํ์ฌ ๋ณต์ก๋๊ฐ ์๋ชจ๋๋ค. |
| 52 | + - ์ต์
์ ๊ฒฝ์ฐ(`skewed tree`), ๋์ด๋ `n`๊ฐ์ด๋ฏ๋ก `O(n)`์ ์๋ชจํ๋ค. |
| 53 | + - ์ต์ ์ ๊ฒฝ์ฐ(`perfect binary tree`), ๋์ด๋ `log(n)`๊ฐ ์ด๋ฏ๋ก `O(log(n))`์ ์๋ชจํ๋ค. |
| 54 | + |
| 55 | +# Code |
| 56 | +```go |
| 57 | +func isValidBSTFrom(root *TreeNode, min, max int) bool { |
| 58 | + if root == nil { |
| 59 | + return true |
| 60 | + } |
| 61 | + if !(min < root.Val && root.Val < max) { |
| 62 | + return false |
| 63 | + } |
| 64 | + return isValidBSTFrom(root.Left, min, root.Val) && isValidBSTFrom(root.Right, root.Val, max) |
| 65 | +} |
| 66 | + |
| 67 | +func isValidBST(root *TreeNode) bool { |
| 68 | + return isValidBSTFrom(root, math.MinInt, math.MaxInt) |
| 69 | +} |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +# Learned |
| 74 | +- `math` ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ `MinInt`, `MaxInt` ์ฌ์ฉ๋ฒ |
| 75 | + - `MinInt`๋ `-1 << 31`๋ก ๊ณ์ฐํ๋ค. |
| 76 | + - ์ `-`์ ์ฌํํธํ๋์ง ๊ถ๊ธํด์ ์ฐพ์๋ดค๋ค. |
| 77 | + - ๋นํธ ๋งจ๋ค๊ฐ 1์ธ ์๋ค์ `<< 31`์ ํ๋ฉด ๋ชจ๋ `MinInt`๋ฅผ ๋ง๋ค ์ ์์ง๋ง, `1 << 31`๋ก `MaxInt`์ ๋๋น๋๋๋ก `-1`์ ์ ํํ ๊ฒ ๊ฐ๋ค. ๋ค๋ฅธ ์๋ค์ ๋นํด ์ข ๋ ์ง๊ด์ ์ด๊ธฐ๋ ํ๋ค. |
| 78 | + |
| 79 | +- `...` operator (Three dots, Ellipsis) |
| 80 | + - unpacking์ ํ๋ ์ฉ๋์ด๋ค. |
| 81 | + - ์ฌ๋ก |
| 82 | + - ํจ์ ์ธ์๋ก ๋ช ๊ฐ์ธ์ง ์ ์ ์๋ ์ธ์๋ค์ด ๋ค์ด์ฌ ๋ |
| 83 | + - ๋ฐฐ์ด ์ด๊ธฐํ ์ ๊ธธ์ด๋ฅผ ๋ชฐ๋ผ ์ปดํ์ผ๋ฌ์๊ฒ ๋งก๊ธฐ๊ณ ์ถ์ ๋ |
| 84 | + - ์ฐธ๊ณ : [๋งํฌ](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/) |
0 commit comments