98. Validate Binary Search Tree

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. Example 1:

    2
   / \
  1   3

Binary tree [2,1,3], return true.

Example 2:

    1
   / \
  2   3

Binary tree [1,2,3], return false.

Related issue: 333 Largest BST Subtree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);        
    }

    private boolean isValidBST(TreeNode root, long min, long max){
        if(root == null) return true;

        if((root.val > min && root.val < max)){
            return isValidBST(root.left, min, (long)root.val) 
                    && isValidBST(root.right, (long)root.val, max);
        }else{
            return false;
        }
    }
}

What if Long is not available in the system.

Pre Order traversal

public class Solution {
    List<Integer> nodes = new ArrayList<>();
    public boolean isValidBST(TreeNode root) {
         if(root == null) return true;

        boolean leftIsValid = isValidBST(root.left);

        if(nodes.size() > 0 && nodes.get(nodes.size()-1) >= root.val ){
            return false;
        }

        nodes.add(root.val);

        boolean rightIsValid = isValidBST(root.right);

        return leftIsValid && rightIsValid;
    }

}

You notice that you don't really need the array for pre-order traversing, you only need the previous node.

THIS IS A VERY IMPORTANT TECHNIQUE while traversing he tree, keep tracking of visited nodes.

public class Solution {
    TreeNode prev = null;
    public boolean isValidBST(TreeNode root) {
         if(root == null) return true;

        boolean leftIsValid = isValidBST(root.left);
        if(!leftIsValid) return false;
        // you can return from here is left is not valid.
        if(prev != null && prev.val >= root.val ){
            return false;
        }

        prev = root;

        boolean rightIsValid = isValidBST(root.right);

        return leftIsValid && rightIsValid;
    }

}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isValidBST(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode prev = null;
        while(!stack.isEmpty() || root != null){
            while(root != null){
                stack.push(root);
                root = root.left; 
            }
            TreeNode top = stack.pop();
            if(prev != null && top.val <= prev.val) return false;
            else prev = top;
            root = top.right;
        }

        return true;
    }
}

results matching ""

    No results matching ""