Friday, September 12, 2014

Leetcode: Validate Binary Search Tree @Python

# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param root, a tree node
    # @return a boolean
    def iterVBST(self,root,m,M):
        if root==None: return True
        if root.val>=M or root.val<=m: return False
        return self.iterVBST(root.left,m,root.val) and self.iterVBST(root.right,root.val,M)
        
    def isValidBST(self, root):
        return self.iterVBST(root,-2147483648,2147483647)

No comments :

Post a Comment