Check if the Binary Tree contains a balanced BST of size K

Given a Binary Tree and a positive integer K. The task is to check whether the Balanced BST of size K exist in a given Binary Tree or not. If it exists then print “Yes” else print “No”.


Input: K = 4,
Below is the given Tree:
       /    \
      10     26
     /  \     / \
    5   12  25  40
           /   /  \
          20  35   50
Output: Yes
Subtree of the given tree with
size k is given below:
       /  \
      35   50

Input: K = 4,
Below is the given Tree:
          / \
         7   10
Output: No
There is no subtree of size K
which forms a balanced BT.

Approach: The idea is to use Post Order Traversal. Following are the steps for solving the problem:

  1. Perform a Post Order Traversal on the given tree and check BST condition for each node where the largest value in the left sub-tree should be smaller than the current value and the smaller value in the right sub tree should be greater the current value.
  2. Then check if the BST is balanced or not that is the absolute difference between left and right sub-tree should be either 0 or 1.
  3. Then pass values return from the sub-trees to the parent.
  4. Perform the above steps for all node and take the boolean variable ans which is initially marked false which is used to check whether a balanced BST is present or not.
  5. If a Balanced BST of size K is found then print “Yes” else print “No”.

Below is the implementation of above approach:






// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// A tree node
struct node {
    int data;
    node* left;
    node* right;
// Structure of temporary variable
struct minMax {
    bool isBST;
    bool balanced;
    int size;
    int hight;
    int min;
    int max;
// Function to create the node
node* createNode(int value)
    node* temp = new node();
    temp->left = NULL;
    temp->right = NULL;
    temp->data = value;
// Utility function to find Balanced
// BST of size k
minMax findBalancedBstUtil(node* root,
                           int k, bool& ans)
    // Base condition
    if (root == NULL)
        return { true, true, 0, 0,
                 INT_MAX, INT_MIN };
    // Temporary variable
    minMax temp;
    // Recursive call for left sub-tree
    minMax lsTree
        = findBalancedBstUtil(root->left,
                              k, ans);
    if (ans == true)
        return temp;
    // Recursive call for right sub-tree
    minMax rsTree
        = findBalancedBstUtil(root->right,
                              k, ans);
    if (ans == true)
        return temp;
    // Check those conditions which
    // violated the rules of BST
    if (!lsTree.isBST || !rsTree.isBST
        || lsTree.max > root->data
        || rsTree.min < root->data) {
        temp.isBST = false;
        return temp;
    // Check whether the Bst is
    // height balanced or not
    if (abs(lsTree.hight
            - rsTree.hight)
            == 1
        || abs(lsTree.hight
               - rsTree.hight)
               == 0)
        temp.balanced = true;
        temp.balanced = false;
    // Make the variable true
    // as sub-tree is BST
    temp.isBST = true;
    // Store the size
    temp.size = 1 + lsTree.size
                + rsTree.size;
    // Store the height
    temp.hight = max(lsTree.hight,
                 + 1;
    // Store the minimum of BST
    temp.min = root->left != NULL
                   ? lsTree.min
                   : root->data;
    // Store the maximum of BST
    temp.max = root->right != NULL
                   ? rsTree.max
                   : root->data;
    // Condition to check whether the
    // size of Balnced BST is K or not
    if (temp.balanced == true
        && temp.size == k) {
        ans = true;
    // Return the temporary variable
    // with updated data
    return temp;
// Function to find the Balanced
// BST of size k
string findBalancedBst(node* root,
                       int k)
    bool ans = false;
    // Utility function call
    findBalancedBstUtil(root, k, ans);
    return ans == true ? "Yes" : "No";
// Driver Code
int main()
    // Given Binary Tree
    node* root = createNode(15);
    root->left = createNode(10);
    root->right = createNode(26);
    root->left->left = createNode(5);
    root->left->right = createNode(12);
    root->right->left = createNode(25);
    root->right->left->left = createNode(20);
    root->right->right = createNode(40);
    root->right->right->left = createNode(35);
    root->right->right->right = createNode(50);
    root->right->right->right->right = createNode(60);
    int k = 4;
    // Function Call
    cout << findBalancedBst(root, k);
    return 0;




Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai