Print Leaf Nodes at a given Level
Last Updated :
16 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
struct node {
struct node* left;
struct node* right;
int data;
};
struct node* newNode( int data)
{
struct node* temp = new node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
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);
}
}
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
class GFG
{
static class node
{
node left;
node right;
int data;
};
static node newNode( int data)
{
node temp = new node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
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 );
}
}
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);
}
}
|
Python3
class node:
def __init__( self , data):
self .left = None
self .right = None
self .data = data
def newNode(data):
return node(data)
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);
|
C#
using System;
class GFG
{
public class node
{
public node left;
public node right;
public int data;
};
static node newNode( int data)
{
node temp = new node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
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);
}
}
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);
}
}
|
Javascript
<script>
class node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let temp = new node(data);
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
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>
|
Complexity Analysis:
- Time Complexity: O(N) where N is the number of nodes in a binary tree.
- Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...