Skip to content
Related Articles

Related Articles

Count nodes with sum of path made by only left child nodes at least K

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Sep, 2022
View Discussion
Improve Article
Save Article

Given a binary tree and an integer K, the task is to write a program to count the number of nodes such that the path from the current node to a leaf consisting of only the left child of nodes has a sum greater than or equal to K.

Examples:

Input: K = 15,
Tree: 
                       8
                 /          \
              9             10
         /     \           /    \
     11      12    13    7
   /   \      /        /      /   \
 6    9   6      7     15   11

Output: 4
Explanation: Nodes with left children sum ≥ ‘K’ are :
Node 8 = 9 + 11 + 6 = 26
Node 9 = 11 + 6 = 17
Node 10 = 13 + 7 = 20
Node 7 = 15

Input:  K = 20,  
Tree: 
                   3
              /       \
          16     10 

Output: 0
Explanation: No nodes with left children sum ≥ 20

Approach: To solve the problem follow the below idea:

One simple approach is to find the sum of left children nodes for every node and compare it with K.

Follow the steps to solve the problem:

  • Maintain counter with name count
  • Implement a recursive function leftNodeSum that recursively calculates the sum of the left nodes for every node in the tree
  • Use a recursive function to traverse through every node
  • Check the sum with K, if it is greater than K then increment the count.
  • Return the count as the answer. 

Below is the implementation of the above approach.

C++




// C++ code for the above approach:
  
#include <bits/stdc++.h>
using namespace std;
  
// Node structure
struct node {
    int data;
    struct node* left;
    struct node* right;
    node(int x)
    {
        this->data = x;
        this->left = this->right = NULL;
    }
};
  
// Function to count left node sum
int leftNodeSum(struct node* head)
{
    if (!head)
        return 0;
    return head->data + leftNodeSum(head->left);
}
  
// Function to traverse the tree
int traverseNodes(struct node* head, int k, int* count)
{
    if (!head)
        return 0;
  
    int sum = leftNodeSum(head->left);
    if (sum >= k)
        (*count)++;
  
    traverseNodes(head->left, k, count);
    traverseNodes(head->right, k, count);
}
  
// Driver code
int main()
{
    int K = 15;
    int count = 0;
    struct node* start = NULL;
    start = new node(8);
    start->left = new node(9);
    start->right = new node(10);
    start->left->left = new node(11);
    start->left->right = new node(12);
    start->right->left = new node(13);
    start->right->right = new node(7);
    start->left->left->left = new node(6);
    start->left->left->right = new node(9);
    start->left->right->left = new node(6);
    start->right->left->left = new node(7);
    start->right->right->left = new node(15);
    start->right->right->right = new node(11);
  
    // Function call
    traverseNodes(start, K, &count);
  
    cout << count << endl;
    return 0;
}

C




// C codde to implement the approach
  
#include <stdio.h>
#include <stdlib.h>
  
// Structure of a tree node
struct node {
    int data;
    struct node* left;
    struct node* right;
};
int count = 0;
  
// Function to add a new node
struct node* addNode(int data)
{
    struct node* n
        = (struct node*)malloc(sizeof(struct node));
    n->data = data;
    n->left = NULL;
    n->right = NULL;
    return n;
}
  
// Function to find the sum of left children
int leftNodeSum(struct node* head)
{
    if (!head)
        return 0;
    return head->data + leftNodeSum(head->left);
}
  
// Function to traverse the tree
int traverseNodes(struct node* head, int k)
{
    if (!head)
        return 0;
  
    int sum = leftNodeSum(head->left);
    if (sum >= k)
        count++;
  
    traverseNodes(head->left, k);
    traverseNodes(head->right, k);
}
  
// Driver code
void main()
{
    int K = 15;
    struct node* start = NULL;
    start = addNode(8);
    start->left = addNode(9);
    start->right = addNode(10);
    start->left->left = addNode(11);
    start->left->right = addNode(12);
    start->right->left = addNode(13);
    start->right->right = addNode(7);
    start->left->left->left = addNode(6);
    start->left->left->right = addNode(9);
    start->left->right->left = addNode(6);
    start->right->left->left = addNode(7);
    start->right->right->left = addNode(15);
    start->right->right->right = addNode(11);
  
    // Function call
    traverseNodes(start, K);
  
    printf("%d", count);
}

Python3




# Python3 code ot implement the approach
  
# Class to define the structure of a tree node
class node:
    def __init__(self):
        self.data = None;
        self.left = None;
        self.right = None;
  
count = 0
  
# Function to add a new node
def addNode(data):
    n = node()
    n.data = data
    n.left = None
    n.right = None
    return n
  
# Function to find the sum of left children
def leftNodeSum(head):
    if not head:
        return 0
    return head.data + leftNodeSum(head.left)
  
# Function to traverse the tree
def traverseNodes(head, k):
    global count
    if not head:
        return 0
    sum = leftNodeSum(head.left)
    if sum >= k:
        count+= 1
    traverseNodes(head.left, k)
    traverseNodes(head.right, k)
  
# Driver code
if __name__ == '__main__':
    K = 15
    start = None
    start = addNode(8)
    start.left = addNode(9)
    start.right = addNode(10)
    start.left.left = addNode(11)
    start.left.right = addNode(12)
    start.right.left = addNode(13)
    start.right.right = addNode(7)
    start.left.left.left = addNode(6)
    start.left.left.right = addNode(9)
    start.left.right.left = addNode(6)
    start.right.left.left = addNode(7)
    start.right.right.left = addNode(15)
    start.right.right.right = addNode(11)
      
    # Function call
    traverseNodes(start, K)
    print(count)

Javascript




class node {
constructor(){
let data;
let left;
let right;
}
  
};
let count = 0;
  
function addNode(data){
let n = new node();
n.data = data;
n.left = null;
n.right = null;
return n;
}
  
function leftNodeSum(head){
if(!head)return 0;
return head.data + leftNodeSum(head.left);
}
  
function traverseNodes(head, k){
if(!head)return 0;
  
let sum = leftNodeSum(head.left);
if(sum >= k)count++;
  
traverseNodes(head.left, k);
traverseNodes(head.right, k);
  
}
  
  
let k = 15;
let start = null;
start = addNode(8);
start.left = addNode(9);
start.right = addNode(10);
start.left.left = addNode(11);
start.left.right = addNode(12);
start.right.left = addNode(13);
start.right.right = addNode(7);
start.left.left.left = addNode(6);
start.left.left.right = addNode(9);
start.left.right.left = addNode(6);
start.right.left.left = addNode(7);
start.right.right.left = addNode(15);
start.right.right.right = addNode(11);
  
  
traverseNodes(start, k);
  
console.log(count);

Output

4

Time Complexity: O(N)
Auxiliary Space: O(N) the space used by the recursion stack


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!