Open in App
Not now

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

• Last Updated : 30 Sep, 2022

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
{
return 0;
}

// Function to traverse the tree
int traverseNodes(struct node* head, int k, int* count)
{
return 0;

if (sum >= 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 code 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* 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
{
return 0;
}

// Function to traverse the tree
int traverseNodes(struct node* head, int k)
{
return 0;

if (sum >= k)
count++;

}

// Driver code
void main()
{
int K = 15;
struct node* start = NULL;

// Function call
traverseNodes(start, K);

printf("%d", count);
}```

Java

```// Java code for the above approach:
import java.io.*;

// Node structure
class node {
int data;
node left, right;
node(int x)
{
this.data = x;
this.left = this.right = null;
}
}

class GFG {

// Function to count left node sum
{
return 0;
}

// Function to traverse the tree
public static void traverseNodes(node head, int k,
int count[])
{
return;

if (sum >= k)
count[0]++;

}

// Driver Code
public static void main(String[] args)
{
int K = 15;
int count[] = new int[1];
count[0] = 0;
node 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);

System.out.println(count[0]);
}
}

// This code is contributed by Rohit Pradhan```

Python3

```# Python3 code to 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
n = node()
n.data = data
n.left = None
n.right = None
return n

# Function to find the sum of left children
return 0

# Function to traverse the tree
global count
return 0
if sum >= k:
count+= 1

# Driver code
if __name__ == '__main__':
K = 15
start = None

# Function call
traverseNodes(start, K)
print(count)```

C#

```// Include namespace system
using System;

// Node structure
public class node
{
public int data;
public node left;
public node right;
public node(int x)
{
this.data = x;
this.left = this.right = null;
}
}
public class GFG
{

// Function to count left node sum
{
{
return 0;
}
}

// Function to traverse the tree
public static void traverseNodes(node head, int k, int[] count)
{
{
return;
}
if (sum >= k)
{
count[0]++;
}
}

// Driver Code
public static void Main(String[] args)
{
var K = 15;
int[] count = new int[1];
count[0] = 0;
var 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
GFG.traverseNodes(start, K, count);
Console.WriteLine(count[0]);
}
}

// This code is contributed by aadityaburujwale.```

Javascript

```class node {
constructor(){
let data;
let left;
let right;
}

};
let count = 0;

let n = new node();
n.data = data;
n.left = null;
n.right = null;
return n;
}

}

if(sum >= k)count++;

}

let k = 15;
let start = null;

traverseNodes(start, k);

console.log(count);

```
Output

`4`

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

My Personal Notes arrow_drop_up
Related Articles