Print all nodes in a binary tree having K leaves
Last Updated :
11 Sep, 2023
Given a binary tree and a integer value K, the task is to find all nodes in given binary tree having K leaves in subtree rooted with them.
Examples :
// For above binary tree
Input : k = 2
Output: {3}
// here node 3 have k = 2 leaves
Input : k = 1
Output: {6}
// here node 6 have k = 1 leave
Here any node having K leaves means sum of leaves in left subtree and in right subtree must be equal to K. So to solve this problem we use Postorder traversal of tree. First we calculate leaves in left subtree then in right subtree and if sum is equal to K, then print current node. In each recursive call we return sum of leaves of left subtree and right subtree to it’s ancestor.
Algorithm:
- start
- Set the base condition i.e if the node is null then return 0.
- Set the condition if left and right both child nodes are zero then return 1.
- Make a recursive call to find out the total child node of each node present in the tree.
- If the total child node of a specific node is equal to the given number then print the value of the node.
- End
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data ;
struct Node * left, * right ;
};
struct Node * newNode( int data)
{
struct Node * node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int kLeaves( struct Node *ptr, int k)
{
if (ptr == NULL)
return 0;
if (ptr->left == NULL && ptr->right == NULL)
return 1;
int total = kLeaves(ptr->left, k) +
kLeaves(ptr->right, k);
if (k == total)
cout << ptr->data << " " ;
return total;
}
int main()
{
struct Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(4);
root->left->left = newNode(5);
root->left->right = newNode(6);
root->left->left->left = newNode(9);
root->left->left->right = newNode(10);
root->right->right = newNode(8);
root->right->left = newNode(7);
root->right->left->left = newNode(11);
root->right->left->right = newNode(12);
kLeaves(root, 2);
return 0;
}
|
Java
public class GfG {
static class Node
{
int data ;
Node left, right ;
Node( int data)
{
this .data = data;
}
Node()
{
}
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static int kLeaves(Node ptr, int k)
{
if (ptr == null )
return 0 ;
if (ptr.left == null && ptr.right == null )
return 1 ;
int total = kLeaves(ptr.left, k) + kLeaves(ptr.right, k);
if (k == total)
System.out.print(ptr.data + " " );
return total;
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 4 );
root.left.left = newNode( 5 );
root.left.right = newNode( 6 );
root.left.left.left = newNode( 9 );
root.left.left.right = newNode( 10 );
root.right.right = newNode( 8 );
root.right.left = newNode( 7 );
root.right.left.left = newNode( 11 );
root.right.left.right = newNode( 12 );
kLeaves(root, 2 );
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def kLeaves(ptr, k):
if (ptr = = None ):
return 0
if (ptr.left = = None and
ptr.right = = None ):
return 1
total = kLeaves(ptr.left, k) + \
kLeaves(ptr.right, k)
if (k = = total):
print (ptr.data, end = " " )
return total
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 4 )
root.left.left = newNode( 5 )
root.left.right = newNode( 6 )
root.left.left.left = newNode( 9 )
root.left.left.right = newNode( 10 )
root.right.right = newNode( 8 )
root.right.left = newNode( 7 )
root.right.left.left = newNode( 11 )
root.right.left.right = newNode( 12 )
kLeaves(root, 2 )
|
C#
using System;
class GfG
{
public class Node
{
public int data ;
public Node left, right ;
public Node( int data)
{
this .data = data;
}
public Node()
{
}
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static int kLeaves(Node ptr, int k)
{
if (ptr == null )
return 0;
if (ptr.left == null && ptr.right == null )
return 1;
int total = kLeaves(ptr.left, k) + kLeaves(ptr.right, k);
if (k == total)
Console.Write(ptr.data + " " );
return total;
}
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(4);
root.left.left = newNode(5);
root.left.right = newNode(6);
root.left.left.left = newNode(9);
root.left.left.right = newNode(10);
root.right.right = newNode(8);
root.right.left = newNode(7);
root.right.left.left = newNode(11);
root.right.left.right = newNode(12);
kLeaves(root, 2);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .left= this .right= null ;
}
}
function kLeaves(ptr,k)
{
if (ptr == null )
return 0;
if (ptr.left == null && ptr.right == null )
return 1;
let total = kLeaves(ptr.left, k) + kLeaves(ptr.right, k);
if (k == total)
document.write(ptr.data + " " );
return total;
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(4);
root.left.left = new Node(5);
root.left.right = new Node(6);
root.left.left.left = new Node(9);
root.left.left.right = new Node(10);
root.right.right = new Node(8);
root.right.left = new Node(7);
root.right.left.left = new Node(11);
root.right.left.right = new Node(12);
kLeaves(root, 2);
</script>
|
Time complexity : O(n) where n is the number of nodes in given tree.
Auxiliary Space: O(h) where h is the height of given Binary Tree due to Recursion.
Share your thoughts in the comments
Please Login to comment...