The Wayback Machine - https://web.archive.org/web/20211214183118/https://www.geeksforgeeks.org/count-of-nodes-with-average-of-left-subtree-at-least-k-in-a-given-binary-tree/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count of nodes with average of left subtree at least K in a given Binary Tree

  • Last Updated : 14 Dec, 2021

Given a binary tree and a number K, the task is to count the number of nodes having the average of the values in their left subtree greater than or equal to K.

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input :  K=5
Tree:    
               2
           /     \
        5        4
      / \       /  \
   5   6    6   2
    \       /
    5    4
Output: 3
Explanation:  
               2         ——– level 0
           /     \                        
        5        4       ——– level 1
      / \       /  \                    
   5   6      6   2     ——– level 2
    \         /                          
    5      4            ——– level 3



Left left subtree average for root node 2 = (5+5+5) / 3 = 5
Left left subtree average for node 4 at level 1 = (6+4) / 2 = 5
Left left subtree average for node 5 at level 1 = (5+5) / 2 = 5
So, these 3 nodes satisfy the given conditions.

Input: K = 4,  
Tree:           1
                /
              2
            /
          3
        /
      4
Output: 1

 

Approach: Follow the steps below to solve this problem:

  1. Create a global variable ans to store the answer and initialise it with 0.
  2. Create a function countHelper which will accept a tree node and integer K as the parameter, and will return a pair of the sum of nodes and number of nodes in the subtree of that node.
  3. Now, pass the root node and K in the initial call of this function.
  4. In each call of this recursive function:
    1. Check if the current node is a leaf node or not. If it is a leaf node, just return {0, 0} as both the sum and number of nodes below this node is 0.
    2. Now, call for left and right subtrees.
    3. Check if for the current node, (the sum of left and right subtree/ number of nodes in left and right subtree)>=K, if it is increment ans by 1.
  5. After the recursive function ends, print ans.

Below is the implementation of the above approach.

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Structure of a tree node
class Node {
public:
    int data;
    Node *left, *right;
    Node(int d)
    {
        data = d;
        left = right = NULL;
    }
};
  
// Global variable to store the node count
int ans = 0;
  
// Function to count the nodes in a tree
// with average of all left nodes
// greater than or equal to K .
pair<int, int> countHelper(Node* root,
                           int K)
{
  
    // For leaf Node
    if (!root->left and !root->right) {
        return { root->data, 1 };
    }
  
    pair<int, int> left = { 0, 0 },
                   right = { 0, 0 };
  
    // For left subtree
    if (root->left) {
        left = countHelper(root->left, K);
    }
  
    // For right subtree
    if (root->right) {
        right = countHelper(root->right, K);
    }
    if (left.second != 0
        and left.first / left.second >= K) {
        ans += 1;
    }
  
    return { left.first + right.first + root->data,
             left.second + right.second + 1 };
}
  
// Function to call the initial
// instance of function countHelper
void countNodes(Node* root, int K)
{
    countHelper(root, K);
    cout << ans;
}
  
// Driver Code
int main()
{
    // Given Tree
    Node* root = new Node(2);
    root->left = new Node(5);
    root->right = new Node(4);
    root->left->left = new Node(5);
    root->left->right = new Node(6);
    root->right->left = new Node(6);
    root->right->right = new Node(2);
    root->left->left->right = new Node(5);
    root->right->left->left = new Node(4);
  
    int K = 5;
  
    countNodes(root, K);
    return 0;
}
Output
3

Time Complexity: O(N) where N is the number of nodes in the tree
Auxiliary Space: O(N)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!