Print all nodes in a binary tree having K leaves

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.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count all nodes having k leaves
// in subtree rooted with them
#include<bits/stdc++.h>
using namespace std;
  
/* A binary tree node  */
struct Node
{
    int data ;
    struct Node * left, * right ;
};
  
/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct Node * newNode(int data)
{
    struct Node * node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
  
// Function to print all nodes having k leaves
int kLeaves(struct Node *ptr,int k)
{
    // Base Conditions : No leaves
    if (ptr == NULL)
        return 0;
  
    // if node is leaf
    if (ptr->left == NULL && ptr->right == NULL)
        return 1;
  
    // total leaves in subtree rooted with this
    // node
    int total = kLeaves(ptr->left, k) +
                kLeaves(ptr->right, k);
  
    // Print this node if total is k
    if (k == total)
        cout << ptr->data << " ";
  
    return total;
}
  
// Driver program to run the case
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count all nodes having k leaves 
// in subtree rooted with them 
class GfG {
  
/* A binary tree node */
static class Node 
    int data ; 
    Node left, right ;
    Node(int data)
    {
        this.data = data;
    }
    Node()
    {
          
    }
}
  
/* Helper function that allocates a new node with the 
given data and NULL left and right pointers. */
static Node newNode(int data) 
    Node node = new Node(); 
    node.data = data; 
    node.left = null;
    node.right = null
    return (node); 
  
// Function to print all nodes having k leaves 
static int kLeaves(Node ptr,int k) 
    // Base Conditions : No leaves 
    if (ptr == null
        return 0
  
    // if node is leaf 
    if (ptr.left == null && ptr.right == null
        return 1
  
    // total leaves in subtree rooted with this 
    // node 
    int total = kLeaves(ptr.left, k) + kLeaves(ptr.right, k); 
  
    // Print this node if total is k 
    if (k == total) 
        System.out.print(ptr.data + " "); 
  
    return total; 
  
// Driver program to run the case 
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); 
  

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count all nodes having k leaves 
// in subtree rooted with them 
using System;
      
class GfG 
{
  
/* A binary tree node */
public class Node 
    public int data ; 
    public Node left, right ;
    public Node(int data)
    {
        this.data = data;
    }
    public Node()
    {
          
    }
}
  
/* Helper function that allocates a new node with the 
given data and NULL left and right pointers. */
static Node newNode(int data) 
    Node node = new Node(); 
    node.data = data; 
    node.left = null;
    node.right = null
    return (node); 
  
// Function to print all nodes having k leaves 
static int kLeaves(Node ptr,int k) 
    // Base Conditions : No leaves 
    if (ptr == null
        return 0; 
  
    // if node is leaf 
    if (ptr.left == null && ptr.right == null
        return 1; 
  
    // total leaves in subtree rooted with this 
    // node 
    int total = kLeaves(ptr.left, k) + kLeaves(ptr.right, k); 
  
    // Print this node if total is k 
    if (k == total) 
        Console.Write(ptr.data + " "); 
  
    return total; 
  
// Driver program to run the case 
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); 
  
}
  
// This code has been contributed by 29AjayKumar

chevron_right



Output:

5 7

Time complexity : O(n)

This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.