Check If the Binary Tree is Binary Search Tree or Not

A binary search tree is a descendent of a more general binary tree with some constraints. In a binary search tree, the arrangement of nodes should follow certain properties. These properties are:

  • All the parent nodes of the tree should have greater value than the child node of the left subtree.
  • All the parent nodes should have a value smaller than that of the child node of the right subtree.

Each node's left and right subtree should also follow the two above properties of the Binary Search Tree.

The Binary Search Tree does not contain any duplicate value.

In this tutorial, we will see if the binary tree, through different ways, is a binary search tree or not. We will look at the various possible solutions.

Approach - 1

The most basic idea to solve this problem is to check for each node if the nodes in the left subtree are smaller than the parent node and the nodes in the right subtree are greater than the parent node. We will use a recursive function to solve this problem.

Below is the algorithmic approach to solve the problem:

  • First, we will state the base condition that if the current node of the tree is empty or null, then we will return true. An empty node is a binary search tree.
  • Then we will check for the left child. If the value of the current parent node is smaller than or equal to (binary search tree should have distinct elements) the value of the child node in the left subtree, we will return false.
  • The next step is to check the condition for the right subtree. If the current parent node's value is greater than or equal to the child node of the right subtree's value, then we will return false.
  • Then we will recursively call the function for the current node's left and right subtrees. If either function calls will return false, then we will return false.
  • If the function reaches this point, that signifies all the conditions are satisfied, and the binary tree is a binary search tree. Hence, we will return true.

Below is the Python code of the above method.

Code

Output:

The given tree is a BST

Time Complexity: O(N2) is the time complexity of this method. The time complexity is non-linear because the primary function to check the conditions of the binary tree visits every node; hence, O(N) and the functions to return the minimum and the maximum values also visit every node taking O(N) time. Hence the final time complexity is O(N) * O(N) = O(N^2).

Space Complexity: The space complexity is O(H). Here H symbolizes the height of the given binary tree. This extra space is taken by the recursion stack.

Approach - 2

This approach's idea is to create a function that will take the node, minimum, and maximum values. This function will traverse the tree from top to bottom, keeping track of the range in which the node value should be present. Each time the minimum value and the maximum value will be updated so that we can check if the binary tree follows the conditions of the binary search tree. This method will visit each node only once. The initial values of the min_val and max_val should be the minimum and maximum integer a node can possibly hold. This will be the range of the root node, as the root node can hold any value. After each iteration, the range will be narrowed accordingly for the left and right subtrees.

We cannot use this method if the tree consists of duplicate elements having the min_val and the max_val.

This is the algorithmic approach to solve the problem:-

  • We will call the helper function isBST() in the primary function. We will give this function the root node, the min_val, and the max_val, i.e., minimum and maximum integer values.
  • If the current node is None, the function will return a True
  • If the minimum value the current node is allowed to have is greater than the node value or the other way, if the value of the current node is greater than the maximum value it can have, then the function will return False
  • Then we will call this function recursively for the left and the right subtrees. But now we have to change the values of the min_val and max_val for the left and the right subtree, respectively. We will understand how to change these values in the code.

Below is the implementation of the above approach:

Code

Output:

The given tree is a BST

Time Complexity: This approach has a linear time complexity i.e., O(N). Here N is the number of nodes present in the binary tree.

Space Complexity: The program will take O(H). The space is required to store the recursive function stack.

Using the In-order Traversal to Check if the Binary Tree is BST or Not

This approach will use the in-order traversal to determine whether the given binary tree is a Binary Search Tree. There is a property that the in-order traversal of a binary search tree follows. This property is that the in-order traversal list of the binary search tree is always in ascending order due to the arrangement of the nodes in the BST. So, we will perform the in-order traversal and then check whether the output is in ascending order. If the output is not in ascending order, the given binary tree is not a Binary Search Tree.

Below is the algorithmic approach to solving the problem using the approach mentioned above:

  • Firstly, we will perform the in-order traversal of the given binary tree. We will store this output in an array.
  • We are assuming that the binary tree does not have any duplicate values.
  • Next, we will run a loop to check whether the output array has elements arranged in ascending order.

Code

Output:

The given tree is a BST

Time Complexity: This approach also has linear time complexity, i.e., O(N), Where N is the number of nodes in the tree

Space Complexity: The program will take O(H). The space is required to store the recursive function stack.

We can reduce the space complexity further by not creating the array. We can keep track of the last element and check whether the current node value is greater than the previous node value. If not, then we will stop the loop and return False. If the loop is complete, the binary tree is a binary search tree.

Code

Output:

The given tree is a BST

Using the Morris Traversal to Find if the Binary Tree is a Binary Search Tree or Not

Below is the algorithmic approach to solving the problem using the Morris Traversal

  • Firstly, we will check if the current node is null or not. If the current is not null, then we will proceed further.
  • We will check if the current node has a left child or not. If it does not have a left child, then we will process the current node and move to the right child of the current node.
    • But, if the current node consists of a left child, then we will find the in-order predecessor of this node. The in-order predecessor is the node present in the rightmost position of the left subtree. Then we will check if the value of the in-order predecessor is less than the value of the current parent node.
  • If the right child of the in-order predecessor is null, then we will set the pointer to the current node and proceed to the left child of the current parent node.
    • If, however, the right child of the in-order predecessor is pointing toward the current node, then we will set it as null. This is done so that we can restore the original structure of the given binary tree. We will process the parent node and then move to the right child.
  • We will iteratively repeat the above steps until we reach the end of the tree and the current node is null.
  • If the loop completes without breaking in between, the binary search tree laws are not violated, and the given tree is a binary search tree.

Below is the Python code of the approach mentioned above

Code

Output:

The binary tree is a valid BST