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:
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:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Simple Recursive solution to check whether BST contains dead end
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Find the closest element in Binary Search Tree | Space Efficient Method
- Inorder predecessor and successor for a given key in BST | Iterative Approach
- Check for Symmetric Binary Tree (Iterative Approach)
- Iterative approach to check if a Binary Tree is Perfect
- Iterative approach to check for children sum property in a Binary Tree
- A program to check if a binary tree is BST or not
- Check if a Binary Tree (not BST) has duplicate values
- Check if the Binary Tree contains a balanced BST of size K
- Largest number less than or equal to N in BST (Iterative Approach)
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)
- K'th Largest Element in BST when modification to BST is not allowed
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST | Set-2
- Two nodes of a BST are swapped, correct the BST
- Find k-th smallest element in BST (Order Statistics in BST)
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.