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 the same as the given level, then check whether the current node is a leaf node or not. If it is a leaf node then print it.
Below is the implementation of the above approach:Â
C++
// 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; } |
Java
// 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 |
Python3
# Python3 program to print all the # leaf nodes at a given level # in a Binary tree Â
# Binary tree node class node:          def __init__( self , data):         self .left = None         self .right = None         self .data = data   # Function to create a new # Binary node def newNode(data):     return node(data)   # Function to print Leaf Nodes at # a given level def PrintLeafNodes(root, level):     if (root = = None ):         return       if (level = = 1 ):         if (root.left = = None and             root.right = = None ):             print (root.data, end = " " )          elif (level > 1 ):         PrintLeafNodes(root.left, level - 1 )         PrintLeafNodes(root.right, level - 1 )          if __name__ = = "__main__" :          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 );     level = 4 ;     PrintLeafNodes(root, level); Â
# This code is contributed by rutvik_56 |
C#
// 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 |
Javascript
<script> Â
    // JavaScript program to print all the     // leaf nodes at a given level     // in a Binary tree          // Binary tree node     class node     {         constructor(data) {            this .left = null ;            this .right = null ;            this .data = data;         }     }          // Function to create a new     // Binary node     function newNode(data)     {         let temp = new node(data);           temp.data = data;         temp.left = null ;         temp.right = null ;           return temp;     }       // Function to print Leaf Nodes at     // a given level     function PrintLeafNodes(root, level)     {         if (root == null )         {             return ;         }           if (level == 1)         {             if (root.left == null && root.right == null )             {                 document.write(root.data + " " );             }           }         else if (level > 1)         {             PrintLeafNodes(root.left, level - 1);             PrintLeafNodes(root.right, level - 1);         }     }          let 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); Â
    let level = 4; Â
    PrintLeafNodes(root, level); Â
</script> |
8 7
Complexity Analysis:
- Time Complexity: O(N) where N is the number of nodes in a binary tree.
- Auxiliary Space: O(N)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!