Skip to content
Related Articles

Related Articles

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

Improve Article
Save Article
Like Article
  • Last Updated : 24 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:

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;
}


Java




// Java code for the above approach
import java.util.*;
 
class GFG{
 
static class pair
{
    int first, second;
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
   
// Structure of a tree node
static class Node {
    int data;
    Node left, right;
    Node(int d)
    {
        data = d;
        left = right = null;
    }
};
 
// Global variable to store the node count
static int ans = 0;
 
// Function to count the nodes in a tree
// with average of all left nodes
// greater than or equal to K .
static pair countHelper(Node root,
                           int K)
{
 
    // For leaf Node
    if (root.left==null && root.right==null) {
        return new pair( root.data, 1 );
    }
 
    pair left = new pair( 0, 0 ),
                   right = new pair( 0, 0 );
 
    // For left subtree
    if (root.left!=null) {
        left = countHelper(root.left, K);
    }
 
    // For right subtree
    if (root.right!=null) {
        right = countHelper(root.right, K);
    }
    if (left.second != 0
        && left.first / left.second >= K) {
        ans += 1;
    }
 
    return new pair( left.first + right.first + root.data,
             left.second + right.second + 1 );
}
 
// Function to call the initial
// instance of function countHelper
static void countNodes(Node root, int K)
{
    countHelper(root, K);
    System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
   
    // 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);
}
}
 
// This code is contributed by shikhasingrajput


Python3




# Python code for the above approach
class pair:
    def __init__(self, first, second):
        self.first = first;
        self.second = second;
 
# Structure of a tree node
class Node:
    def __init__(self, d):
        self.data = d;
        self.left = self.right = None;
 
# Global variable to store the node count
ans = 0;
 
# Function to count the nodes in a tree
# with average of all left nodes
# greater than or equal to K .
def countHelper(root, K):
 
    # For leaf Node
    if (root.left == None and root.right == None):
        return pair(root.data, 1);
 
    left = pair(0, 0)
    right = pair(0, 0)
 
    # For left subtree
    if (root.left != None):
        left = countHelper(root.left, K);
 
    # For right subtree
    if (root.right != None):
        right = countHelper(root.right, K);
     
    if (left.second != 0 and left.first / left.second >= K):
        global ans;
        ans += 1;
 
    return pair(left.first + right.first + root.data, left.second + right.second + 1);
 
# Function to call the initial
# instance of function countHelper
def countNodes(root, K):
    countHelper(root, K);
    print(ans);
 
# Driver Code
# Given Tree
root = Node(2);
root.left = Node(5);
root.right = Node(4);
root.left.left = Node(5);
root.left.right = Node(6);
root.right.left = Node(6);
root.right.right = Node(2);
root.left.left.right = Node(5);
root.right.left.left = Node(4);
 
K = 5;
 
countNodes(root, K);
 
# This code is contributed by Saurabh Jaiswal.


C#




// C# code for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  class pair
  {
    public int first, second;
    public pair(int first, int second) 
    {
      this.first = first;
      this.second = second;
    }   
  }
 
  // Structure of a tree node
  class Node {
    public int data;
    public Node left, right;
    public Node(int d)
    {
      data = d;
      left = right = null;
    }
  };
 
  // Global variable to store the node count
  static int ans = 0;
 
  // Function to count the nodes in a tree
  // with average of all left nodes
  // greater than or equal to K .
  static pair countHelper(Node root,
                          int K)
  {
 
    // For leaf Node
    if (root.left==null && root.right==null) {
      return new pair( root.data, 1 );
    }
 
    pair left = new pair( 0, 0 ),
    right = new pair( 0, 0 );
 
    // For left subtree
    if (root.left!=null) {
      left = countHelper(root.left, K);
    }
 
    // For right subtree
    if (root.right!=null) {
      right = countHelper(root.right, K);
    }
    if (left.second != 0
        && left.first / left.second >= K) {
      ans += 1;
    }
 
    return new pair( left.first + right.first + root.data,
                    left.second + right.second + 1 );
  }
 
  // Function to call the initial
  // instance of function countHelper
  static void countNodes(Node root, int K)
  {
    countHelper(root, K);
    Console.Write(ans);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // 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);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
// Javascript code for the above approach
 
 
class pair {
    constructor(first, second) {
        this.first = first;
        this.second = second;
    }
}
 
// Structure of a tree node
class Node {
 
    constructor(d) {
        this.data = d;
        this.left = this.right = null;
    }
};
 
// Global variable to store the node count
let ans = 0;
 
// Function to count the nodes in a tree
// with average of all left nodes
// greater than or equal to K .
function countHelper(root, K) {
 
    // For leaf Node
    if (root.left == null && root.right == null) {
        return new pair(root.data, 1);
    }
 
    let left = new pair(0, 0),
        right = new pair(0, 0);
 
    // For left subtree
    if (root.left != null) {
        left = countHelper(root.left, K);
    }
 
    // For right subtree
    if (root.right != null) {
        right = countHelper(root.right, K);
    }
    if (left.second != 0
        && left.first / left.second >= K) {
        ans += 1;
    }
 
    return new pair(left.first + right.first + root.data, left.second + right.second + 1);
}
 
// Function to call the initial
// instance of function countHelper
function countNodes(root, K) {
    countHelper(root, K);
    document.write(ans);
}
 
// Driver Code
// Given Tree
let 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);
 
let K = 5;
 
countNodes(root, K);
 
// This code is contributed by Saurabh Jaiswal
</script>


 
 

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!