Print Leaf Nodes at a given Level

Given a Binary tree, print all the leaf nodes of a Binary tree at a given level L.

Examples:

Input:    
           1
          /  \
         2    3
        /    /  \
       4    5    6
 level = 3

Output: 4 5 6

Input:    
             7
            /  \
           2    3
          / \    \
         4   9   10
                 /
                6
   level = 3

Output: 4 9



Approach: Recursively traverse the tree in a level order manner. If the current level is same as the given level, then check whether the current node is leaf node or not. If it is a leaf node then print it.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print all the
// leaf nodes at a given level
// in a Binary tree
#include <bits/stdc++.h>
using namespace std;
  
// Binary tree node
struct node {
    struct node* left;
    struct node* right;
    int data;
};
  
// Function to create a new
// Binary node
struct node* newNode(int data)
{
    struct node* temp = new node;
  
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
  
    return temp;
}
  
// Function to print Leaf Nodes at
// a given level
void PrintLeafNodes(struct node* root, int level)
{
    if (root == NULL)
        return;
  
    if (level == 1) {
        if (root->left == NULL && root->right == NULL)
            cout << root->data << " ";
    }
    else if (level > 1) {
        PrintLeafNodes(root->left, level - 1);
        PrintLeafNodes(root->right, level - 1);
    }
}
  
// Driver code
int main()
{
    struct node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(6);
    root->right->right = newNode(4);
    root->left->left->left = newNode(8);
    root->left->left->right = newNode(7);
  
    int level = 4;
  
    PrintLeafNodes(root, level);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all the 
// leaf nodes at a given level 
// in a Binary tree 
class GFG
{
  
    // Binary tree node 
    static class node
    {
  
        node left;
        node right;
        int data;
    };
  
    // Function to create a new 
    // Binary node 
    static node newNode(int data) 
    {
        node temp = new node();
  
        temp.data = data;
        temp.left = null;
        temp.right = null;
  
        return temp;
    }
  
    // Function to print Leaf Nodes at 
    // a given level 
    static void PrintLeafNodes(node root, int level) 
    {
        if (root == null)
        {
            return;
        }
  
        if (level == 1
        {
            if (root.left == null && root.right == null
            {
                System.out.print(root.data + " ");
            }
  
        
        else if (level > 1
        {
            PrintLeafNodes(root.left, level - 1);
            PrintLeafNodes(root.right, level - 1);
        }
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(6);
        root.right.right = newNode(4);
        root.left.left.left = newNode(8);
        root.left.left.right = newNode(7);
  
        int level = 4;
  
        PrintLeafNodes(root, level);
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all the 
// leaf nodes at a given level 
// in a Binary tree 
using System;
  
class GFG
{
  
    // Binary tree node 
    public class node
    {
  
        public node left;
        public node right;
        public int data;
    };
  
    // Function to create a new 
    // Binary node 
    static node newNode(int data) 
    {
        node temp = new node();
  
        temp.data = data;
        temp.left = null;
        temp.right = null;
  
        return temp;
    }
  
    // Function to print Leaf Nodes at 
    // a given level 
    static void PrintLeafNodes(node root, int level) 
    {
        if (root == null)
        {
            return;
        }
  
        if (level == 1) 
        {
            if (root.left == null && root.right == null
            {
                Console.Write(root.data + " ");
            }
  
        
        else if (level > 1) 
        {
            PrintLeafNodes(root.left, level - 1);
            PrintLeafNodes(root.right, level - 1);
        }
    }
  
    // Driver code 
    public static void Main(String[] args) 
    {
        node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(6);
        root.right.right = newNode(4);
        root.left.left.left = newNode(8);
        root.left.left.right = newNode(7);
  
        int level = 4;
  
        PrintLeafNodes(root, level);
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


Output:

8 7

Time Complexity:O(N) where N is number of nodes in a binary tree



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, 29AjayKumar