Given a binary tree, determine whether it is a BST.

Practice this problem

This problem has a simple recursive solution. The BST property “every node on the right subtree has to be larger than the current node and every node on the left subtree has to be smaller than the current node” is the key to figuring out whether a tree is a BST or not.

The greedy algorithm – traverse the tree, at every node check whether the node contains a value larger than the value at the left child and smaller than the value on the right child – does not work for all cases. Consider the following tree:

      20
     /  \
   10    30
        /  \
       5    40

In the tree above, each node meets the condition that the node contains a value larger than its left child and smaller than its right child hold, and yet it’s not a BST: the value 5 is on the right subtree of the node containing 20, a violation of the BST property.

Instead of deciding based solely on a node’s values and its children, we also need information flowing down from the parent. In the tree above, if we could remember about the node containing the value 20, we would see that the node with value 5 is violating the BST property contract.

So, the condition we need to check at each node is:

  • If the node is the left child of its parent, it must be smaller than (or equal to) the parent, and it must pass down the value from its parent to its right subtree to make sure none of the nodes in that subtree is greater than the parent.
  • If the node is the right child of its parent, it must be larger than the parent, and it must pass down the value from its parent to its left subtree to make sure none of the nodes in that subtree is lesser than the parent.

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

The tree is not a BST!

Java


Download  Run Code

Output:

The tree is not a BST!

Python


Download  Run Code

Output:

The tree is not a BST!

The time complexity of the above solution is O(n), where n is the size of the BST, and requires space proportional to the tree’s height for the call stack.

Another approach:

We know that an inorder traversal of a binary search tree returns the nodes in sorted order. To determine whether a given binary tree is a BST, keep track of the last visited node while traversing the tree. Then for each encountered node in the inorder traversal, check whether the last visited node is smaller (or smaller/equal, if duplicates are to be allowed in the tree) compared to the current node.

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

The tree is not a BST!

Java


Download  Run Code

Output:

The tree is not a BST!

Python


Download  Run Code

Output:

The tree is not a BST!

The time complexity of the above solution is O(n), where n is the size of the BST, and requires space proportional to the tree’s height for the call stack.

 
References: https://en.wikipedia.org/wiki/Binary_search_tree