98 validate binary search tree

AC and Best Solution

1
2
3
4
5
6
7
8
9
10
11
def (self, root, min, max):
if root == None:
return True
if not root.left and not root.right:
return root.val < max and root.val > min
if root.val < min or root.val >= max:
return False
return self.ValidBST(root.left, min, root.val) and self.ValidBST(root.right, root.val, max)

def isValidBST(self, root):
return self.ValidBST(root, -2147483649, 2147483648)

Time complexity: O(n)
Space Complexity: O(1)

loop through all nodes, check if condition match
default min = python min int -1
default max = python max int +1