@@ -0,0 +1,1440 @@
+package com.interview.tree;%0A%0A/**%0A * Date 04/11/2015%0A * @author tusroy%0A * %0A * Given a binary tree, return true if it is binary search tree else return false.%0A * %0A * Solution%0A * Keep min, max for every recursion. Initial min and max is very small and very larger%0A * number. Check if root.data is in this range. When you go left pass min and root.data and %0A * for right pass root.data and max.%0A * %0A * Time complexity is O(n) since we are looking at all nodes.%0A * %0A * Test cases:%0A * Null tree%0A * 1 or 2 nodes tree%0A * Binary tree which is actually BST%0A * Binary tree which is not a BST%0A */%0Apublic class IsBST %7B%0A%0A    public boolean isBST(Node root)%7B%0A        return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);%0A    %7D%0A    %0A    private boolean isBST(Node root, int min, int max)%7B%0A        if(root == null)%7B%0A            return true;%0A        %7D%0A        if(root.data %3C= min %7C%7C root.data %3E max)%7B%0A            return false;%0A        %7D%0A        return isBST(root.left, min, root.data) && isBST(root.right, root.data, max);%0A    %7D%0A    %0A    public static void main(String args%5B%5D)%7B%0A        BinaryTree bt = new BinaryTree();%0A        Node root = null;%0A        root = bt.addNode(10, root);%0A        root = bt.addNode(15, root);%0A        root = bt.addNode(-10, root);%0A        root = bt.addNode(17, root);%0A        root = bt.addNode(20, root);%0A        root = bt.addNode(0, root);%0A        %0A        IsBST isBST = new IsBST();%0A        assert isBST.isBST(root);%0A    %7D%0A%7D%0A

Is Binary tree BST