Given a binary tree, we need to write a program to print all leaf nodes of the given binary tree from left to right. That is, the nodes should be printed in the order they appear from left to right in the given tree.
For Example,

For the above binary tree, the output will be as shown below:
4 6 7 9 10
The idea to do this is similar to DFS algorithm. Below is a step by step algorithm to do this:
- Check if the given node is null. If null, then return from the function.
- Check if it is a leaf node. If the node is a leaf node, then print its data.
- If in the above step, the node is not a leaf node then check if the left and right children of node exist. If yes then call the function for left and right child of the node recursively.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
void printLeafNodes(Node *root)
{
if (!root)
return ;
if (!root->left && !root->right)
{
cout << root->data << " " ;
return ;
}
if (root->left)
printLeafNodes(root->left);
if (root->right)
printLeafNodes(root->right);
}
Node* newNode( int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int main()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(8);
root->right->left->left = newNode(6);
root->right->left->right = newNode(7);
root->right->right->left = newNode(9);
root->right->right->right = newNode(10);
printLeafNodes(root);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
public int data;
public Node left, right;
};
static void printLeafNodes(Node root)
{
if (root == null )
return ;
if (root.left == null &&
root.right == null )
{
System.out.print(root.data + " " );
return ;
}
if (root.left != null )
printLeafNodes(root.left);
if (root.right != null )
printLeafNodes(root.right);
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void main(String []args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.right.left = newNode( 5 );
root.right.right = newNode( 8 );
root.right.left.left = newNode( 6 );
root.right.left.right = newNode( 7 );
root.right.right.left = newNode( 9 );
root.right.right.right = newNode( 10 );
printLeafNodes(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def printLeafNodes(root: Node) - > None :
if ( not root):
return
if ( not root.left and
not root.right):
print (root.data,
end = " " )
return
if root.left:
printLeafNodes(root.left)
if root.right:
printLeafNodes(root.right)
if __name__ = = "__main__" :
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.right.left = Node( 5 )
root.right.right = Node( 8 )
root.right.left.left = Node( 6 )
root.right.left.right = Node( 7 )
root.right.right.left = Node( 9 )
root.right.right.right = Node( 10 )
printLeafNodes(root)
|
C#
using System;
class GFG{
class Node
{
public int data;
public Node left, right;
};
static void printLeafNodes(Node root)
{
if (root == null )
return ;
if (root.left == null &&
root.right == null )
{
Console.Write(root.data + " " );
return ;
}
if (root.left != null )
printLeafNodes(root.left);
if (root.right != null )
printLeafNodes(root.right);
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void Main()
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(8);
root.right.left.left = newNode(6);
root.right.left.right = newNode(7);
root.right.right.left = newNode(9);
root.right.right.right = newNode(10);
printLeafNodes(root);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
};
function printLeafNodes(root)
{
if (root == null )
return ;
if (root.left == null &&
root.right == null )
{
document.write(root.data + " " );
return ;
}
if (root.left != null )
printLeafNodes(root.left);
if (root.right != null )
printLeafNodes(root.right);
}
function newNode(data)
{
var temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(8);
root.right.left.left = newNode(6);
root.right.left.right = newNode(7);
root.right.right.left = newNode(9);
root.right.right.right = newNode(10);
printLeafNodes(root);
</script>
|
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)
Iterative Method:
Following is a simple stack-based iterative method to print the leaf nodes from left to right.
- Create an empty stack ‘st’ and push the root node to stack.
- Do the following while stack is not empty.
- Pop an item from the stack
- If the node is a leaf node then print it.
- Else:
- If the right node is not NULL
- push the right node into the stack
- If the left node is not NULL
- push the left node into the stack
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void printleafNodes(Node* root)
{
if (!root)
return ;
stack<Node*> st;
st.push(root);
while (!st.empty()) {
root = st.top();
st.pop();
if (!root->left && !root->right)
cout << root->data << " " ;
if (root->right)
st.push(root->right);
if (root->left)
st.push(root->left);
}
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(8);
root->right->left->left = newNode(6);
root->right->left->right = newNode(7);
root->right->right->left = newNode(9);
root->right->right->right = newNode(10);
printleafNodes(root);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static class Node {
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
static void printleafNodes(Node root)
{
if (root == null )
return ;
Stack<Node> st = new Stack<>();
st.push(root);
while (!st.isEmpty()) {
root = st.peek();
st.pop();
if (root.left == null && root.right == null )
System.out.print(root.data + " " );
if (root.right != null )
st.push(root.right);
if (root.left != null )
st.push(root.left);
}
}
public static void main(String[] args)
{
Node root = new Node( 1 );
root.left = new Node( 2 );
root.right = new Node( 3 );
root.left.left = new Node( 4 );
root.right.left = new Node( 5 );
root.right.right = new Node( 8 );
root.right.left.left = new Node( 6 );
root.right.left.right = new Node( 7 );
root.right.right.left = new Node( 9 );
root.right.right.right = new Node( 10 );
printleafNodes(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(data):
temp = Node(data)
return temp
def printleafNodes(root):
if not root:
return
st = []
st.append(root)
while len (st) > 0 :
root = st.pop()
if not root.left and not root.right:
print (root.data, end = " " )
if root.right:
st.append(root.right)
if root.left:
st.append(root.left)
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.right.left = newNode( 5 )
root.right.right = newNode( 8 )
root.right.left.left = newNode( 6 )
root.right.left.right = newNode( 7 )
root.right.right.left = newNode( 9 )
root.right.right.right = newNode( 10 )
printleafNodes(root)
|
C#
using System;
using System.Collections.Generic;
class GFG {
class Node {
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
static void printleafNodes(Node root)
{
if (root == null )
return ;
Stack<Node> st = new Stack<Node>();
st.Push(root);
while (st.Count != 0) {
root = st.Peek();
st.Pop();
if (root.left == null && root.right == null )
Console.Write(root.data + " " );
if (root.right != null )
st.Push(root.right);
if (root.left != null )
st.Push(root.left);
}
}
public static void Main( string [] args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(8);
root.right.left.left = new Node(6);
root.right.left.right = new Node(7);
root.right.right.left = new Node(9);
root.right.right.right = new Node(10);
printleafNodes(root);
}
}
|
Javascript
class Node {
constructor(data){
this .data=data;
this .left= null ;
this .right= null ;
}
}
function printleafNodes(root)
{
if (!root)
return ;
let st=[];
st.push(root);
while (st.length>0) {
root = st[st.length-1];
st.pop();
if (!root.left && !root.right)
document.write(root.data + " " );
if (root.right)
st.push(root.right);
if (root.left)
st.push(root.left);
}
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(8);
root.right.left.left = new Node(6);
root.right.left.right = new Node(7);
root.right.right.left = new Node(9);
root.right.right.right = new Node(10);
printleafNodes(root);
|
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.