Skip to content

Commit 557a5af

Browse files
committed
validate-bst solution
1 parent db8e2de commit 557a5af

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
3+
"""
4+
DFS로 각 노드가 low < node.val < high 조건을 만족하는지 확인한다.
5+
low와 high는 각각 왼쪽과 오른쪽 서브트리에서의 최대값과 최소값을 나타내며, 재귀적으로 확인한다.
6+
"""
7+
def dfs(node, low, high):
8+
if not node:
9+
return True
10+
if not (low < node.val < high):
11+
return False
12+
13+
return dfs(node.left, low, node.val) and dfs(node.right, node.val, high)
14+
15+
return dfs(root, float('-inf'), float('inf'))

0 commit comments

Comments
 (0)