Print leaf nodes in binary tree from left to right using one stack in C++

C++Server Side ProgrammingProgramming

Program should print the leaf nodes of a binary tree from left to right but the challenge involves is using of only one stack

Through push() function insert nodes of a binary tree and through pop() operation display the leaf nodes.

Leaf nodes are the end nodes whose left and right pointer is NULL which means that particular node is not a parent node.


Input : 12 21 32 41 59 33 70
Output : 41 59 33 70

Stack is a data structure which is a LIFO structure in which top pointer will point to the last elements inserted so the leaf nodes will be inserted at the last into the stack and they will be pop out or removed from the stack before any other node as per stack structure.

The below code shows the C++ implementation using STL of the algorithm given.


   Step 1 -> create node variable of type structure
      Declare int data
      Declare pointer of type node using *left, *right
   Step 2 -> create function for inserting node with parameter as val
      Declare node variable of node using malloc
      Set node->data = val
      Set node->left = node->right = NULL
      return node
   step 3 -> Declare Function void leaf(Node *ptr)
      create vector stack<Node*>stck
      Loop While 1
      IF ptr
         Ptr = ptr->left
         IF (stck.empty())
            IF (>right == NULL)
               Set ptr =
               Set stck.pop()
               IF ptr->left = NULL
                  Print ptr->data
            Loop While ptr ==>right
               Set ptr =
               Call stck.pop()
               IF stck.empty()
               IF !stck.empty()
                  Set ptr =>right
                  Set ptr = NULL
   Step 4-> In main()
      Call New passing value user want to insert as Node* root = New(12)
      Call leaf(root)


#include <bits/stdc++.h>
using namespace std;
// Structure of a node
struct Node {
   Node* left;
   Node* right;
   int data;
//Function to create a new node
Node* New(int val) {
   Node* node = new Node();
   node->left = node->right = NULL;
   node->data = val;
   return node;
// leaf node using stack
void leaf(Node* ptr) {
   // stack that will store nodes
   stack<Node*> stck;
   while (1) {
      if (ptr) {
         ptr = ptr->left;
      } else {
         if (stck.empty())
         else {
            if (>right == NULL) {
               ptr =;
               // Print the leaf node
               if (ptr->left == NULL)
                  printf("%d ", ptr->data);
            while (ptr ==>right) {
               ptr =;
               if (stck.empty())
            if (!stck.empty())
               ptr =>right;
               ptr = NULL;
int main() {
   printf("leaf nodes at end level are : ");
   Node* root = New(12);
   root->left = New(21);
   root->right = New(32);
   root->left->left = New(41);
   root->left->right = New(59);
   root->right->left = New(33);
   root->right->right = New(70);
   return 0;


If we run above program then it will generate following output.

leaf nodes at end level are : 41 59 33 70
Published on 22-Aug-2019 12:10:21