// Given a binary tree, determine if it is a valid binary search tree (BST).

// Assume a BST is defined as follows:

// The left subtree of a node contains only nodes with keys less than the node's key.
// The right subtree of a node contains only nodes with keys greater than the node's key.
// Both the left and right subtrees must also be binary search trees.

/**
 * 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 helper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    public boolean helper(TreeNode node, int min, int max){
    	if(node == null) return true;

    	if(node.val <= min || node.val >= max){
    		return false;
    	}else{
    		return helper(node.left, min, node.val) && helper(node.right, node.val, max);
    	}
    }
}
@@ -941,8 +941,1615 @@
 %0A    %7D%0A%7D
+%0A%0A%0A/**%0A * Definition for binary tree%0A * public class TreeNode %7B%0A *     int val;%0A *     TreeNode left;%0A *     TreeNode right;%0A *     TreeNode(int x) %7B val = x; %7D%0A * %7D%0A */%0Apublic class Solution %7B%0A    class Data %7B%0A        boolean isValid;%0A        int min;%0A        int max;%0A        Data(int min, int max, boolean isValid) %7B%0A            this.min = min;%0A            this.max = max;%0A            this.isValid = isValid;%0A        %7D%0A    %7D%0A%0A    public boolean isValidBST(TreeNode root) %7B%0A        Data res = helper(root);%0A        return res == null %7C%7C res.isValid;%0A    %7D%0A%0A    public Data helper(TreeNode node) %7B%0A        if (node == null) %7B%0A            return null;%0A        %7D%0A%0A        Data left = helper(node.left);%0A        Data right = helper(node.right);%0A        Data res = new Data(0,0, false);%0A%0A       // left invalid%0A        if (left != null && !left.isValid) %7B%0A            return res;%0A        %7D%0A        // right invalid%0A        if (right != null && !right.isValid) %7B%0A            return res;%0A        %7D%0A%0A        if (left == null) %7B%0A            res.min = node.val;%0A        %7D else %7B%0A            if (left.max %3E= node.val %7C%7C left.min %3E= node.val) %7B%0A                res.isValid = false;%0A                return res;%0A            %7D else %7B%0A                res.min = left.min;%0A            %7D%0A        %7D%0A%0A        if (right == null) %7B%0A            res.max = node.val;%0A        %7D else %7B%0A            if (right.min %3C= node.val ) %7B%0A%0A                res.isValid = false;%0A                return res;%0A            %7D else %7B%0A                res.max = right.max;%0A            %7D%0A        %7D%0A%0A        res.isValid = true;%0A%0A        return res;%0A    %7D%0A%7D

update valid BST recursion method without using Integer property.