-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathWHYjun.py
More file actions
17 lines (16 loc) · 674 Bytes
/
WHYjun.py
File metadata and controls
17 lines (16 loc) · 674 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
# https://stackoverflow.com/a/37300370
return self.isValid(root, float('-inf'), float('inf'))
def isValid(self, node: Optional[TreeNode], low: float, high: float):
if node is None:
return True
if node.val <= low or node.val >= high:
return False
return self.isValid(node.left, low, node.val) and self.isValid(node.right, node.val, high)