Suraj_IOI's blog

By Suraj_IOI, history, 8 months ago, In English,

This program checks whether a Binary tree is a BST or not, can someone explain me the working of recursive return statement in the function isBSTUtil

public class BinaryTree {

Node root; 

boolean isBST() 
    return isBSTUtil(root, Integer.MIN_VALUE, 
boolean isBSTUtil(Node node, int min, int max) 
    if (node == null) 
        return true; 

   if ( < min || > max) 
        return false; 

    return (isBSTUtil(node.left, min, && 
            isBSTUtil(node.right,, max)); 

public static void main(String args[]) 
    BinaryTree tree = new BinaryTree(); 
    tree.root = new Node(4); 
    tree.root.left = new Node(2); 
    tree.root.right = new Node(5); 
    tree.root.left.left = new Node(1); 
    tree.root.left.right = new Node(3); 

    if (tree.isBST()) 
        System.out.println("IS BST"); 
        System.out.println("Not a BST"); 

} class Node { int data; Node left, right;

public Node(int item) 
    data = item; 
    left = right = null; 


Read more »