Print leaf nodes in binary tree from left to right using one stack
Given a binary tree, the task is 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.
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 iterative approach using two stacks.
Approach:The idea is to perform iterative postorder traversal using one stack and print the leaf nodes.
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 printLeafLeftToRight(Node* p)
{
stack<Node*> s;
while (1) {
if (p) {
s.push(p);
p = p->left;
}
else {
if (s.empty())
break ;
else {
if (s.top()->right == NULL) {
p = s.top();
s.pop();
if (p->left == NULL)
printf ( "%d " , p->data);
}
while (p == s.top()->right) {
p = s.top();
s.pop();
if (s.empty())
break ;
}
if (!s.empty())
p = s.top()->right;
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);
printLeafLeftToRight(root);
return 0;
}
|
Java
import java.util.*;
class GfG
{
static class Node
{
Node left;
Node right;
int data;
}
static Node newNode( int key)
{
Node node = new Node();
node.left = null ;
node.right = null ;
node.data = key;
return node;
}
static void printLeafLeftToRight(Node p)
{
Stack<Node> s = new Stack<Node> ();
while ( true )
{
if (p != null )
{
s.push(p);
p = p.left;
}
else
{
if (s.isEmpty())
break ;
else
{
if (s.peek().right == null )
{
p = s.peek();
s.pop();
if (p.left == null )
System.out.print(p.data + " " );
}
while (p == s.peek().right)
{
p = s.peek();
s.pop();
if (s.isEmpty())
break ;
}
if (!s.isEmpty())
p = s.peek().right;
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 );
printLeafLeftToRight(root);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def printLeafLeftToRight(p):
s = []
while ( 1 ):
if (p):
s.insert( 0 , p)
p = p.left
else :
if len (s) = = 0 :
break
else :
if (s[ 0 ].right = = None ):
p = s[ 0 ]
s.pop( 0 )
if (p.left = = None ):
print (p.data, end = " " )
while (p = = s[ 0 ].right):
p = s[ 0 ]
s.pop( 0 )
if len (s) = = 0 :
break
if len (s):
p = s[ 0 ].right
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 )
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 key)
{
Node node = new Node();
node.left = null ;
node.right = null ;
node.data = key;
return node;
}
static void printLeafLeftToRight(Node p)
{
Stack<Node> s = new Stack<Node> ();
while ( true )
{
if (p != null )
{
s.Push(p);
p = p.left;
}
else
{
if (s.Count == 0)
break ;
else
{
if (s.Peek().right == null )
{
p = s.Peek();
s.Pop();
if (p.left == null )
Console.Write(p.data + " " );
}
while (p == s.Peek().right)
{
p = s.Peek();
s.Pop();
if (s.Count == 0)
break ;
}
if (s.Count != 0)
p = s.Peek().right;
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);
printLeafLeftToRight(root);
}
}
|
Javascript
<script>
class Node
{
constructor(key) {
this .left = null ;
this .right = null ;
this .data = key;
}
}
function newNode(key)
{
let node = new Node(key);
return node;
}
function printLeafLeftToRight(p)
{
let s = [];
while ( true )
{
if (p != null )
{
s.push(p);
p = p.left;
}
else
{
if (s.length == 0)
break ;
else
{
if (s[s.length - 1].right == null )
{
p = s[s.length - 1];
s.pop();
if (p.left == null )
document.write(p.data + " " );
}
while (p == s[s.length - 1].right)
{
p = s[s.length - 1];
s.pop();
if (s.length == 0)
break ;
}
if (s.length != 0)
p = s[s.length - 1].right;
else
p = null ;
}
}
}
}
let 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);
printLeafLeftToRight(root);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
06 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...