Finding if a Binary Tree is a Binary Search Tree
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Overview
A Binary Search Tree (BST) is a unique form of a binary tree where each node follows a particular order: the left child contains nodes with values less than the parent node, and the right child contains nodes with values greater than the parent node. Verifying whether a given binary tree satisfies these conditions to qualify as a BST is a common problem in computer science.
This article delves into methods for determining if a binary tree is a BST, provides technical explanations, and discusses key considerations in evaluating a tree's properties.
Key Concepts
Binary Tree vs. Binary Search Tree
- Binary Tree: A tree data structure where each node has at most two children.
- Binary Search Tree (BST): A binary tree wherein:
- All nodes in the left subtree have values less than the root's value.
- All nodes in the right subtree have values greater than the root's value.
- Both left and right subtrees are also BSTs.
Properties of a BST
- In-Order Traversal: The in-order traversal of a BST produces a sorted sequence of values.
- Unique Elements: Nodes in a BST have unique keys (no duplicate values).
Methods to Determine if a Binary Tree is a BST
Recursive Approach
To determine if a binary tree is a BST, the recursive approach verifies each node's value within a specific range. The process involves these steps:
- Verify the Current Node: Ensure the current node's value falls between a defined minimum and maximum range.
- Recur for Subtrees:
- For the left subtree, update the maximum bound (now the current node’s value).
- For the right subtree, update the minimum bound.
Algorithm
- Unique Values: A valid BST should not contain duplicate values.
- Edge Cases: A single node or an empty tree should return
Trueas it fulfills the BST properties by default. - Efficiency: The recursive approach uses extra space but may reach a space complexity of due to the recursive call stack. The in-order method operates iteratively with time complexity.
- Morris Traversal: A space-efficient variation of in-order traversal that uses threading to keep space complexity .
- Augmented Tree: Utilize data structures with additional information that keeps track of subtree properties.
Related reading
- Finding if a string is an iterative substring Algorithm in C?
- finding if two words are anagrams of each other
- Finding items in an universal hash table?
- Finding K-nearest neighbors and its implementation
- Finding kth-shortest paths?
- Finding kth smallest number from n sorted arrays
- Finding length of shortest cycle in undirected graph
- finding long repeated substrings in a massive string

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.