6/02/2014

90. Validate Binary Search Tree

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isValidBST(TreeNode root) {
        return check(root, Integer.MAX_VALUE, Integer.MIN_VALUE);
    }
   
    public boolean check(TreeNode root, int max, int min) {
        if (root==null) return true;
        if (root.val>=max || root.val<=min) return false;
       
        return check(root.left, root.val, min) && check(root.right, max, root.val);
    }
}

没有评论:

发表评论