Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
Last Updated :
06 Aug, 2021
Given a Binary Tree, the task is to print the leaf nodes from left to right. The nodes must be printed in the order they appear from left to right.
Examples:
Input :
1
/ \
2 3
/ \ / \
4 5 6 7
Output :4 5 6 7
Input :
4
/ \
5 9
/ \ / \
8 3 7 2
/ / \
12 6 1
Output :12 3 7 6 1
We have already discussed the Recursive approach. Here we will solve the problem using two stacks.
Approach:The idea is to use two stacks, one to store all the nodes of the tree and the other one to store all the leaf nodes. We will pop the top node of the first stack. If the node has a left child, we will push it on top of the first stack, if it has a right child then we will push it onto the top of the first stack, but if the node is a leaf node then we will push it onto the top of the second stack. We will do it for all the nodes until we have traversed the Binary tree completely.
Then we will start popping the second stack and print all its elements until the stack gets 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 data)
{
Node* temp = new Node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
void PrintLeafLeftToRight(Node* root)
{
stack<Node*> s1;
stack<Node*> s2;
s1.push(root);
while (!s1.empty()) {
Node* curr = s1.top();
s1.pop();
if (curr->left)
s1.push(curr->left);
if (curr->right)
s1.push(curr->right);
else if (!curr->left && !curr->right)
s2.push(curr);
}
while (!s2.empty()) {
cout << s2.top()->data << " " ;
s2.pop();
}
}
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(7);
root->left->left->left = newNode(10);
root->left->left->right = newNode(11);
root->right->right->left = newNode(8);
PrintLeafLeftToRight(root);
return 0;
}
|
Java
import java.util.*;
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 PrintLeafLeftToRight(Node root)
{
Stack<Node> s1 = new Stack<>();
Stack<Node> s2 = new Stack<>();;
s1.push(root);
while (!s1.empty())
{
Node curr = s1.peek();
s1.pop();
if (curr.left!= null )
s1.push(curr.left);
if (curr.right!= null )
s1.push(curr.right);
else if (curr.left== null && curr.right== null )
s2.push(curr);
}
while (!s2.empty())
{
System.out.print(s2.peek().data + " " );
s2.pop();
}
}
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( 7 );
root.left.left.left = newNode( 10 );
root.left.left.right = newNode( 11 );
root.right.right.left = newNode( 8 );
PrintLeafLeftToRight(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def PrintLeafLeftToRight(root):
s1 = []
s2 = []
s1.append(root)
while len (s1) ! = 0 :
curr = s1.pop()
if curr.left:
s1.append(curr.left)
if curr.right:
s1.append(curr.right)
elif not curr.left and not curr.right:
s2.append(curr)
while len (s2) ! = 0 :
print (s2.pop().data, end = " " )
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( 7 )
root.left.left.left = Node( 10 )
root.left.left.right = Node( 11 )
root.right.right.left = Node( 8 )
PrintLeafLeftToRight(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 data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
static void PrintLeafLeftToRight(Node root)
{
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();;
s1.Push(root);
while (s1.Count != 0)
{
Node curr = s1.Peek();
s1.Pop();
if (curr.left != null )
s1.Push(curr.left);
if (curr.right != null )
s1.Push(curr.right);
else if (curr.left == null && curr.right == null )
s2.Push(curr);
}
while (s2.Count != 0)
{
Console.Write(s2.Peek().data + " " );
s2.Pop();
}
}
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(7);
root.left.left.left = newNode(10);
root.left.left.right = newNode(11);
root.right.right.left = newNode(8);
PrintLeafLeftToRight(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let temp = new Node(data);
return temp;
}
function PrintLeafLeftToRight(root)
{
let s1 = [];
let s2 = [];
s1.push(root);
while (s1.length > 0)
{
let curr = s1[s1.length - 1];
s1.pop();
if (curr.left!= null )
s1.push(curr.left);
if (curr.right!= null )
s1.push(curr.right);
else if (curr.left== null && curr.right== null )
s2.push(curr);
}
while (s2.length > 0)
{
document.write(s2[s2.length - 1].data + " " );
s2.pop();
}
}
let 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(7);
root.left.left.left = newNode(10);
root.left.left.right = newNode(11);
root.right.right.left = newNode(8);
PrintLeafLeftToRight(root);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...