Skip to content

Commit f46a370

Browse files
committed
[WHYjun] WEEK 02 solutions - (5/5)
1 parent 0e4e164 commit f46a370

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
9+
# https://stackoverflow.com/a/37300370
10+
return self.isValid(root, float('-inf'), float('inf'))
11+
12+
def isValid(self, node: Optional[TreeNode], low: float, high: float):
13+
if node is None:
14+
return True
15+
if node.val <= low or node.val >= high:
16+
return False
17+
return self.isValid(node.left, low, node.val) and self.isValid(node.right, node.val, high)

0 commit comments

Comments
 (0)