
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check if a Binary Tree is a BST
Binary Search Tree is a binary tree data structure in which we have 3 properties −
The left subtree of a binary search tree of a node contains only nodes with keys lesser than the node’s key.
The right subtree of a binary search tree node contains only nodes with keys greater than the node’s key.
The left and right of a subtree each must also be a binary search tree.
Algorithm
Begin function BSTUtill() If node is equals to NULL then Return 1. If data of node is less than minimum or greater than maximum data then Return 0. Traverse left and right sub-trees recursively. End.
Example Code
#include <iostream> #include <cstdlib> #include <climits> using namespace std; struct n { int d; n* l; n* r; }; int BSTUtil(n* node, int min, int max); int isBST(n* node) { return(BSTUtil(node, INT_MIN, INT_MAX)); } int BSTUtil(struct n* node, int min, int max) { if (node==NULL) return 1; if (node->d < min || node->d > max) return 0; return BSTUtil(node->l, min, node->d - 1) && BSTUtil(node->r, node->d + 1, max); } n* newN(int d) { n* nod = new n; nod->d = d; nod->l = NULL; nod->r = NULL; return nod; } int main() { n *root = newN(7); root->l = newN(6); root->r = newN(10); root->l->l = newN(2); root->l->r = newN(4); if (isBST(root)) cout<<"The Given Binary Tree is a BST"<<endl; else cout<<"The Given Binary Tree is not a BST"<<endl; n *root1 = newN(10); root1->l = newN(6); root1->r = newN(11); root1->l->l = newN(2); root1->l->r = newN(7); if (isBST(root1)) cout<<"The Given Binary Tree is a BST"<<endl; else cout<<"The Given Binary Tree is not a BST"<<endl; return 0; }
Output
The Given Binary Tree is not a BST The Given Binary Tree is a BST
- Related Articles
- A program to check if a binary tree is BST or not in C ?
- Program to check whether a binary tree is BST or not in Python
- Check if a Binary Tree (not BST) has duplicate value in C++
- Program to find out if a BST is present in a given binary tree in Python
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- Check if a binary tree is subtree of another binary tree in C++
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Largest BST in a Binary Tree in C++
- Check if a given Binary Tree is SumTree in C++
- Check if a given Binary Tree is Heap in Python
- Check if a given Binary Tree is Heap in C++
- C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++

Advertisements