sorindragan
3/19/2018 - 4:16 PM

IsBinaryTree?

A function that checks if a given tree is a BST.

boolean checkBST(Node root) {
        return checkHelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    boolean checkHelper(Node node, int min_left, int max_right) {
        if (node == null) {
            return true;
        }
        
        if (node.data <= min_left || node.data >= max_right) {
            return false;
        }
        
        return checkHelper(node.left, min_left, node.data) && checkHelper(node.right, node.data, max_right);
    }