Print all leaf nodes of a binary tree from right to left
Last Updated :
16 Mar, 2023
Given a binary tree, the task is to print all the leaf nodes of the binary tree from right to left.
Examples:
Input :
1
/ \
2 3
/ \ / \
4 5 6 7
Output : 7 6 5 4
Input :
1
/ \
2 3
/ \ \
4 5 6
/ / \
7 8 9
Output : 9 8 7 4
Recursive Approach: Traverse the tree in Preorder fashion, by first processing the root, then right subtree and then left subtree and do the following:
- Check if the root is null then return from the function.
- If it is a leaf node then print it.
- If not then check if it has right child, if yes then call function for right child of the node recursively.
- Check if it has left child, if yes then call function for left 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;
};
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 ;
if (!root->left && !root->right) {
cout << root->data << " " ;
return ;
}
if (root->right)
printLeafNodes(root->right);
if (root->left)
printLeafNodes(root->left);
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->left->left->left = newNode(8);
root->right->right->left = newNode(9);
root->left->left->left->right = newNode(10);
printLeafNodes(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node left, right;
};
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static void printLeafNodes(Node root)
{
if (root == null )
return ;
if (root.left == null && root.right == null )
{
System.out.print( root.data + " " );
return ;
}
if (root.right != null )
printLeafNodes(root.right);
if (root.left != null )
printLeafNodes(root.left);
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
root.right.left = newNode( 6 );
root.right.right = newNode( 7 );
root.left.left.left = newNode( 8 );
root.right.right.left = newNode( 9 );
root.left.left.left.right = newNode( 10 );
printLeafNodes(root);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def printLeafNodes(root):
if root = = None :
return
if (root.left = = None and
root.right = = None ):
print (root.data, end = " " )
return
if root.right:
printLeafNodes(root.right)
if root.left:
printLeafNodes(root.left)
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.right.left = newNode( 6 )
root.right.right = newNode( 7 )
root.left.left.left = newNode( 8 )
root.right.right.left = newNode( 9 )
root.left.left.left.right = newNode( 10 )
printLeafNodes(root)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
}
public static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
public static void printLeafNodes(Node root)
{
if (root == null )
{
return ;
}
if (root.left == null && root.right == null )
{
Console.Write(root.data + " " );
return ;
}
if (root.right != null )
{
printLeafNodes(root.right);
}
if (root.left != null )
{
printLeafNodes(root.left);
}
}
public static void Main( string [] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.left.left.left = newNode(8);
root.right.right.left = newNode(9);
root.left.left.left.right = newNode(10);
printLeafNodes(root);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .right = null ;
this .left = null ;
}
}
function newNode(data)
{
var temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
function printLeafNodes(root)
{
if (root == null )
{
return ;
}
if (root.left == null && root.right == null )
{
document.write(root.data + " " );
return ;
}
if (root.right != null )
{
printLeafNodes(root.right);
}
if (root.left != null )
{
printLeafNodes(root.left);
}
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.left.left.left = newNode(8);
root.right.right.left = newNode(9);
root.left.left.left.right = newNode(10);
printLeafNodes(root);
</script>
|
Time Complexity: O(N) where N is the number of nodes in given binary tree.
Space Complexity: O(h) where h is the height of binary tree due to recursion.
Iterative Approach: The idea is to perform iterative Postorder traversal using one stack, but in a modified manner, first, we will visit the right subtree and then the left subtree and finally the root node and print the leaf nodes.
Efficient Approach:
- Create a stack ‘s’ to store the nodes.
- Start from the root node and push it onto the stack.
- Traverse to the right child of the current node until it is not null, and push each node onto the stack.
- If the current node’s left child is null, pop the top node from the stack and check if its right child is also null, then print the value of the node.
- If the left child of the top node on the stack is not null, then traverse to its left child and push each node onto the stack until it is not null.
- If the stack is empty, terminate the loop.
- Repeat steps 4 to 6 until the stack is empty.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node {
Node* left;
Node* right;
int data;
};
Node* newNode( int key)
{
Node* node = new Node();
node->left = node->right = NULL;
node->data = key;
return node;
}
void printLeafRightToLeft(Node* p)
{
stack<Node*> s;
while (1) {
if (p) {
s.push(p);
p = p->right;
}
else {
if (s.empty())
break ;
else {
if (s.top()->left == NULL) {
p = s.top();
s.pop();
if (p->right == NULL)
printf ( "%d " , p->data);
}
while (p == s.top()->left) {
p = s.top();
s.pop();
if (s.empty())
break ;
}
if (!s.empty())
p = s.top()->left;
else
p = NULL;
}
}
}
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
printLeafRightToLeft(root);
return 0;
}
|
Java
import java.util.Stack;
class GFG
{
static class Node
{
Node left;
Node right;
int data;
};
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static void printLeafRightToLeft(Node p)
{
Stack<Node> s = new Stack<>();
while ( true )
{
if (p != null )
{
s.push(p);
p = p.right;
}
else
{
if (s.empty())
break ;
else
{
if (s.peek().left == null )
{
p = s.peek();
s.pop();
if (p.right == null )
System.out.print( p.data+ " " );
}
while (p == s.peek().left)
{
p = s.peek();
s.pop();
if (s.empty())
break ;
}
if (!s.empty())
p = s.peek().left;
else
p = null ;
}
}
}
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
root.right.left = newNode( 6 );
root.right.right = newNode( 7 );
printLeafRightToLeft(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(key) :
node = Node( 0 )
node.left = node.right = None
node.data = key
return node
def printLeafRightToLeft(p) :
s = []
while ( True ) :
if (p ! = None ) :
s.append(p)
p = p.right
else :
if ( len (s) = = 0 ) :
break
else :
if (s[ - 1 ].left = = None ) :
p = s[ - 1 ]
s.pop()
if (p.right = = None ) :
print ( p.data, end = " " )
while (p = = s[ - 1 ].left) :
p = s[ - 1 ]
s.pop()
if ( len (s) = = 0 ) :
break
if ( len (s) > 0 ) :
p = s[ - 1 ].left
else :
p = None
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.right.left = newNode( 6 )
root.right.right = newNode( 7 )
printLeafRightToLeft(root)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public Node left;
public Node right;
public int data;
};
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static void printLeafRightToLeft(Node p)
{
Stack<Node> s = new Stack<Node>();
while ( true )
{
if (p != null )
{
s.Push(p);
p = p.right;
}
else
{
if (s.Count == 0)
break ;
else
{
if (s.Peek().left == null )
{
p = s.Peek();
s.Pop();
if (p.right == null )
Console.Write(p.data + " " );
}
while (p == s.Peek().left)
{
p = s.Peek();
s.Pop();
if (s.Count == 0)
break ;
}
if (s.Count != 0)
p = s.Peek().left;
else
p = null ;
}
}
}
}
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
printLeafRightToLeft(root);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .left = null ;
this .right = null ;
this .data = 0;
}
};
function newNode(key)
{
var node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
function printLeafRightToLeft(p)
{
var s = [];
while ( true )
{
if (p != null )
{
s.push(p);
p = p.right;
}
else
{
if (s.length == 0)
break ;
else
{
if (s[s.length - 1].left == null )
{
p = s[s.length - 1];
s.pop();
if (p.right == null )
document.write(p.data + " " );
}
while (p == s[s.length - 1].left)
{
p = s[s.length - 1];
s.pop();
if (s.length == 0)
break ;
}
if (s.length != 0)
p = s[s.length - 1].left;
else
p = null ;
}
}
}
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
printLeafRightToLeft(root);
</script>
|
Time Complexity: O(N), where N is the total number of nodes in the binary tree.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...