Check if a Binary Tree is BST : Simple and Efficient Approach

Given a Binary Tree, the task is to check whether the given binary tree is Binary Search Tree or not.

A binary search tree (BST) is a node-based binary tree data structure which has the following properties.

  • 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.

From the above properties it naturally follows that:



  • Each node (item in the tree) has a distinct key.
  • BST

    We have already discussed different approaches to solve this problem in the previous article.

    In this article, we will discuss a simple yet efficient approach to solve the above problem.

    The idea is to use Inorder traversal and keep track of the previously visited node’s value. Since the inorder traversal of a BST generates a sorted array as output, So, the previous element should always be less than or equals to the current element.

    While doing In-Order traversal, we can keep track of previously visited Node’s value by passing an integer variable using reference to the recursive calls. If the value of the currently visited node is less than the previous value, then the tree is not BST.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to check if a given tree is BST.
    #include <bits/stdc++.h>
    using namespace std;
      
    /* A binary tree node has data, pointer to 
    left child and a pointer to right child */
    struct Node {
        int data;
        struct Node *left, *right;
      
        Node(int data)
        {
            this->data = data;
            left = right = NULL;
        }
    };
      
    // Utility function to check if Binary Tree is BST
    bool isBSTUtil(struct Node* root, int& prev)
    {
        // traverse the tree in inorder fashion and
        // keep track of prev node
        if (root) {
            if (!isBSTUtil(root->left, prev))
                return false;
      
            // Allows only distinct valued nodes
            if (root->data <= prev)
                return false;
      
            // Initialize prev to current
            prev = root->data;
      
            return isBSTUtil(root->right, prev);
        }
      
        return true;
    }
      
    // Function to check if Binary Tree is BST
    bool isBST(Node* root)
    {
        int prev = INT_MIN;
        return isBSTUtil(root, prev);
    }
      
    /* Driver program to test above functions*/
    int main()
    {
        struct Node* root = new Node(5);
        root->left = new Node(2);
        root->right = new Node(15);
        root->left->left = new Node(1);
        root->left->right = new Node(4);
      
        if (isBST(root))
            cout << "Is BST";
        else
            cout << "Not a BST";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Is BST
    

    Time Complexity: O(N)
    Auxiliary Space: O(1)



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.