|
1 | 1 | """
|
2 |
| -Inputs: |
| 2 | +[๋ฌธ์ ํ์ด] |
| 3 | +# Inputs |
| 4 | +TreeNode ํด๋์ค ๊ฐ์ฒด |
3 | 5 |
|
4 |
| -Outputs: |
| 6 | +# Outputs |
| 7 | +valid ์ด์ง ํ์ ํธ๋ฆฌ์ธ์ง์ ๋ํ ์ฌ๋ถ |
5 | 8 |
|
6 |
| -Constraints: |
| 9 | +# Constraints |
| 10 | +The number of nodes in the tree is in the range [1, 104]. |
| 11 | +-2^31 <= Node.val <= 2^31 - 1 |
7 | 12 |
|
8 |
| -Time Complexity: |
| 13 | +# Ideas |
| 14 | +root ๋ฐฐ์ด ๊ธธ์ด ๊ธฐ์ค์ผ๋ก ํ๋ ค๊ณ ํ์ง๋ง, root๋ ๋ฐฐ์ด์ด ์๋ TreeNode ํด๋์ค์ ๊ฐ์ฒด์ด๋ค. |
| 15 | +1. bfs๋ก ๊ตฌํํด๋ณด๊ธฐ |
| 16 | +ํธ๋ฆฌ ๋์ด ๊ธฐ์ค์ผ๋ก ํผ์ง๋ฉด์ ํ์ํ๋ ๋ชจ์์ด๋ผ bfs ์ ํ |
| 17 | +
|
| 18 | +cur = root |
| 19 | +q.append(cur) |
| 20 | +
|
| 21 | +while not q: |
| 22 | + cur_node = q.popleft() |
| 23 | + if cur_node == null: |
| 24 | + continue |
| 25 | +
|
| 26 | + if cur_node.right != null and (cur_node.left.val > cur_node.val or cur_node.right.val < cur_node.val): |
| 27 | + return False |
| 28 | +
|
| 29 | + else: |
| 30 | + q.append(cur_node.left) |
| 31 | +
|
| 32 | +๋จผ๊ฐ ์กฐ๊ฑด๋ฌธ์ด ๊ธธ์ด์ง๋ ๊ฒ ๊ฐ์ ํจ์จ์ ์ธ ํ์ด๋ ์๋ ๊ฒ ๊ฐ์ง๋ง..์ผ๋จ ์๋! |
| 33 | +
|
| 34 | +n์ด node ๊ฐ์๋ผ๋ฉด |
| 35 | +Time: O(n), Space: O(n) |
| 36 | +
|
| 37 | +from collections import deque |
| 38 | +
|
| 39 | +class Solution: |
| 40 | + def isValidBST(self, root: Optional[TreeNode]) -> bool: |
| 41 | + q = deque([]) |
| 42 | + q.append(root) |
| 43 | +
|
| 44 | + while q: |
| 45 | + cur_node = q.popleft() |
| 46 | + if cur_node is None: |
| 47 | + continue |
| 48 | +
|
| 49 | + if cur_node.left is not None and cur_node.left.val >= cur_node.val: |
| 50 | + return False |
| 51 | +
|
| 52 | + elif cur_node.right is not None and cur_node.right.val <= cur_node.val: |
| 53 | + return False |
| 54 | +
|
| 55 | + else: |
| 56 | + q.append(cur_node.left) |
| 57 | + q.append(cur_node.right) |
| 58 | +
|
| 59 | + return True |
| 60 | +
|
| 61 | +- ๋ฐ๋ก ๋ฐ์ |
| 62 | +root = |
| 63 | +[5,4,6,null,null,3,7] |
| 64 | +
|
| 65 | +์ BST๊ฐ ์๋์ง ์ดํด๊ฐ ์๊ฐ๋ค.. |
| 66 | +-> 3์ด 5๋ณด๋ค ์์์ ๊ทธ๋ ๋ค |
| 67 | +๋ฃจํธ ๊ธฐ์ค ์ค๋ฅธ์ชฝ์ ์์ ๊ฐ๋ค๋ ๋ฌด์กฐ๊ฑด ์์์ผํ๋ค. |
| 68 | +๊ทธ๋ผ ํ ๋จ๊ณ ์์ ๊ฐ๋ง ๋ณด๋ฉด ์๋๋ค๋ ๊ฑด๋ฐ, ํ์ ๋ฐฉ๋ฒ ๋ฐ๊ฟ์ผ ํ ๊ฒ ๊ฐ๋ค |
| 69 | +
|
| 70 | +2. ์ฌ๊ท๋ก ๋๋ฉด์, ์ด ๊ฐ๋ณด๋ค๋ ์ปค์ผํ๋ค๋ ๋ฐฐ์ด์ ์ธ์๋ก ํจ๊ป ๋๊ฒจ์ฃผ๊ธฐ? |
| 71 | +ํ์ง๋ง, ์ด๋ dfs์์ ๋ฐฐ์ด ์์๋ค๋ณด๋ค ๋ณธ์ธ ๊ฐ์ด ์ปค์ผํ๋์ง, ์์ผํ๋์ง ๋ชจ๋ฆ!! |
| 72 | +
|
| 73 | +๊ฒฐ๊ตญ ํ์ด ์ฐธ๊ณ |
| 74 | +
|
| 75 | +[ํ๊ณ ] |
9 | 76 |
|
10 |
| -Space Complexity: |
11 | 77 |
|
12 | 78 | """
|
13 | 79 |
|
| 80 | +# Definition for a binary tree node. |
| 81 | +# class TreeNode: |
| 82 | +# def __init__(self, val=0, left=None, right=None): |
| 83 | +# self.val = val |
| 84 | +# self.left = left |
| 85 | +# self.right = right |
| 86 | +from collections import deque |
| 87 | + |
| 88 | +class Solution: |
| 89 | + def isValidBST(self, root: Optional[TreeNode]) -> bool: |
| 90 | + ret = True |
| 91 | + |
| 92 | + def dfs(node, low, high): |
| 93 | + if not node: |
| 94 | + return True |
| 95 | + |
| 96 | + if not (low < node.val < high): |
| 97 | + return False |
| 98 | + |
| 99 | + return dfs(node.left, low, node.val) and dfs(node.right, node.val, high) |
14 | 100 |
|
| 101 | + return dfs(root, float("-inf"), float("inf")) |
0 commit comments