Determine if a given binary tree is binary search tree.
Assumptions
There should no be duplicate keys in binary search tree.
You can assume the keys stored in the binary search tree can not be Integer.MIN_VALUE or Integer.MAX_VALUE.
Corner Cases
What if the binary tree is null? Return true in this case.
class Solution(object):
def isBST(self, root):
if not root:
return True
return self.helper(root,float('-inf'),float('inf'))
def helper(self,root,min,max):
if not root:
return True
if root.val >= max or root.val <= min:
return False
return self.helper(root.left,min,root.val) and self.helper(root.right,root.val,max)
网友评论