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.

Updated on: 07-Mar-2022

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements