Check if a Binary Tree is BST : Simple and Efficient Approach
Last Updated :
22 Jun, 2021
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.
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 and 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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node( int data)
{
this ->data = data;
left = right = NULL;
}
};
bool isBSTUtil( struct Node* root, int & prev)
{
if (root) {
if (!isBSTUtil(root->left, prev))
return false ;
if (root->data <= prev)
return false ;
prev = root->data;
return isBSTUtil(root->right, prev);
}
return true ;
}
bool isBST(Node* root)
{
int prev = INT_MIN;
return isBSTUtil(root, prev);
}
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;
}
|
Java
class GFG {
static int prev = Integer.MIN_VALUE;
static class Node {
int data;
Node left, right;
Node( int data)
{
this .data = data;
left = right = null ;
}
};
static boolean isBSTUtil(Node root)
{
if (root != null ) {
if (!isBSTUtil(root.left))
return false ;
if (root.data <= prev)
return false ;
prev = root.data;
return isBSTUtil(root.right);
}
return true ;
}
static boolean isBST(Node root)
{
return isBSTUtil(root);
}
public static void main(String[] args)
{
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))
System.out.print( "Is BST" );
else
System.out.print( "Not a BST" );
}
}
|
Python3
import math
prev = - math.inf
class Node:
def __init__( self , data):
self .left = None
self .right = None
self .data = data
def checkBST(root):
global prev
if root:
if not checkBST(root.left):
return False
if root.data < prev:
return False
prev = root.data
return checkBST(root.right)
return True
def main():
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 15 )
root.left.left = Node( 1 )
root.left.right = Node( 4 )
if checkBST(root):
print ( "Is BST" )
else :
print ( "Not a BST" )
if __name__ = = '__main__' :
main()
|
C#
using System;
class GFG {
class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
};
static bool isBSTUtil(Node root, int prev)
{
if (root != null ) {
if (!isBSTUtil(root.left, prev))
return false ;
if (root.data <= prev)
return false ;
prev = root.data;
return isBSTUtil(root.right, prev);
}
return true ;
}
static bool isBST(Node root)
{
int prev = int .MinValue;
return isBSTUtil(root, prev);
}
public static void Main(String[] args)
{
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))
Console.Write( "Is BST" );
else
Console.Write( "Not a BST" );
}
}
|
Javascript
<script>
class Node {
constructor(data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
};
function isBSTUtil(root, prev)
{
if (root != null ) {
if (!isBSTUtil(root.left, prev))
return false ;
if (root.data <= prev)
return false ;
prev = root.data;
return isBSTUtil(root.right, prev);
}
return true ;
}
function isBST(root)
{
var prev = -1000000000;
return isBSTUtil(root, prev);
}
var 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))
document.write( "Is BST" );
else
document.write( "Not a BST" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...