- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Largest BST in a Binary Tree in C++

In a binary tree, there are only two nodes (left and right) in each child node. Tree structures are simply representations of data. Binary Search Trees (BSTs) are special types of Binary Trees that meet these conditions −

Compared to its parent, the left child node is smaller

The parent node of the right child is larger than the child node

Suppose we're given a binary tree, and we are supposed to find out what's the largest binary search tree (BST) within it.

In this task, we will create a function to find the largest BST in a binary tree. When the binary tree is itself a BST, it is possible to determine the size of the entire binary tree. As an example −

**Input**

10 /\ 5 15 /\ \ 1 8 7

As shown, the BST Subtree highlighted, in this case, is the largest. '3' is the subtree's size, so the return value is the subtree's size.

**Input**

52 / \ 37 67 /\ / \ 12 27 57 77 /\ 72 87

**Output**

5

Subtrees with nodes whose length is less than the length of their parent nodes have up to three size BST nodes.

**Approach for Finding out the Largest BST in the Given Binary Tree**

For every node x, a binary tree is BST if the following points are valid. Only nodes whose data is less than that of their parent node appear in the left subtree of a node. There can only be one node that has more data than its parent node. Both left and right subtrees should be characterized by binary search trees (BST).

**The algorithm will be −**

We will start from the root of the binary tree and do the in-order traversal using recursion. For the current node 'ROOT,' we will do the following-

If it is the root of a valid BST, we return its size.

Else, we will find the largest BST in the left and right subtrees.

## Example

#include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node *left; struct Node *right; }; struct Node * newNode (int data) { struct Node *node = new Node; node->data = data; node->left = node->right = NULL; return (node); } struct Detail { int size; int max; int min; int ans; bool isBST; }; bool isBST (Node * root, int min, int max) { if (root == NULL) { return true; } if (root->data < min || root->data > max) { return false; } return isBST (root->left, min, root->data - 1) && isBST (root->right, root->data + 1, max); } int size (Node * root) { if (root == NULL) { return 0; } return 1 + size (root->left) + size (root->right); } int largestBST (Node * root) { // Current Subtree is BST. if (isBST (root, INT_MIN, INT_MAX) == true) { return size (root); } // Find largest BST in left and right subtrees. return max (largestBST (root->left), largestBST (root->right)); } int main () { struct Node *root = newNode (67); root->left = newNode (72); root->right = newNode (77); root->left->left = newNode (57); printf ("Size of the largest BST is %d", largestBST (root)); return 0; }

## Output

Size of the largest BST is 2

## Conclusion

In this problem, we learned what a binary tree & a binary search tree is and how to find out the largest BST in the given binary tree with the help of recursion. With the help of recursion, we will find out if a subtree under every node is a BST or not and return the values accordingly.

- Related Articles
- Find the largest BST subtree in a given Binary Tree - Set 1 in C++
- Maximum Sum BST in Binary Tree in C++
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Check if a Binary Tree (not BST) has duplicate value in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- C++ Program to Check if a Binary Tree is a BST
- Largest BST Subtree in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- A program to check if a binary tree is BST or not in C ?
- Find the largest Complete Subtree in a given Binary Tree in C++
- Program to check whether a binary tree is BST or not in Python
- Convert BST to Greater Tree in C++
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in Python
- Program to find out if a BST is present in a given binary tree in Python