1
# Runtime: 89 ms (Top 18.35%) | Memory: 16.5 MB (Top 80.77%)
2
# Definition for a binary tree node.
3
# class TreeNode:
4
# def __init__(self, val=0, left=None, right=None):
5
# self.val = val
6
# self.left = left
7
# self.right = right
8
class Solution:
9
def isValidBST(self, root: Optional[TreeNode]) -> bool:
10

11
def valid(node, left, right):
12
if not node: # checking node is none
13
return True
14
if not (
15
node.val > left and node.val < right
16
): # checking the left value is less than node and right value is greater than node
17
return False
18
return valid(node.left, left, node.val) and valid(
19
node.right, node.val, right
20
) # recursively calling left child and right child and returing the result True if both are true else False
21

22
return valid(
23
root, float("-inf"), float("inf")
24
) # calling recursive function to check

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0