---
title: "Write a program to check if a given binary tree is a binary search tree."  
description: "Write a program to check if a given binary tree is a binary search tree."  
author: "Utpal Vishwas"  
published: 2023-04-21  
updated: 2023-04-22  
canonical: https://www.mindstick.com/forum/157958/write-a-program-to-check-if-a-given-binary-tree-is-a-binary-search-tree  
category: "C Language"  
tags: ["data structure", "programs"]  
reading_time: 2 minutes  

---

# Write a program to check if a given binary tree is a binary search tree.

Write a [program to check](https://www.mindstick.com/forum/157542/write-a-java-program-to-check-if-a-list-of-integers-contains-only-odd-numbers) if a given [binary tree](https://www.mindstick.com/forum/158724/describe-the-process-of-traversing-a-binary-tree-in-pre-order-in-order-and-post-order) is a [binary search](https://www.mindstick.com/forum/159485/working-of-a-binary-search-tree-bst-and-what-is-its-time-complexity-for-insertion-and-retrieval) tree.

## Replies

### Reply by Aryan Kumar

```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Define the binary tree node
struct TreeNode {
   int val;
   struct TreeNode* left;
   struct TreeNode* right;
};
// Function to create a new binary tree node
struct TreeNode* newNode(int data) {
   struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
   node->val = data;
   node->left = NULL;
   node->right = NULL;
   return node;
}
// Function to check if a binary tree is a binary search tree
int isBSTUtil(struct TreeNode* node, int min, int max) {
   if (node == NULL) {
       return 1;
   }
   if (node->val < min || node->val > max) {
       return 0;
   }
   return (isBSTUtil(node->left, min, node->val - 1) && isBSTUtil(node->right, node->val + 1, max));
}
int isBST(struct TreeNode* root) {
   return isBSTUtil(root, INT_MIN, INT_MAX);
}
int main() {
   struct TreeNode* root = newNode(4);
   root->left = newNode(2);
   root->right = newNode(5);
   root->left->left = newNode(1);
   root->left->right = newNode(3);
   if (isBST(root)) {
       printf("The binary tree is a binary search tree.\n");
   } else {
       printf("The binary tree is not a binary search tree.\n");
   }
   return 0;
}
```

In this [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program), we define the [binary](https://www.mindstick.com/forum/34709/please-write-a-program-for-decimal-to-binary-conversion-in-c-sharp) tree node structure and a function to create a new binary tree node. We then define a recursive function **isBSTUtil** that takes a node, a minimum value, and a maximum value as arguments, and returns true if the tree rooted at the node is a binary [search](https://www.mindstick.com/articles/65368/best-smo-services-company-in-hyderabad-improve-search-rankings) tree within the given range. We call this function initially with the root of the binary tree and the minimum and maximum possible integer values. The function returns true if the node is NULL (an empty tree) or if the node's value is within the given range and the left and right subtrees are also binary search trees within their respective ranges. We use INT_MIN and INT_MAX to represent the minimum and maximum integer values respectively.

Finally, we define another function **isBST** that calls **isBSTUtil** with the root of the binary tree and returns the result.

In the **main** function, we create a binary tree and call the **isBST** function to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) it is a binary search tree. We print the result accordingly.


---

Original Source: https://www.mindstick.com/forum/157958/write-a-program-to-check-if-a-given-binary-tree-is-a-binary-search-tree

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
