Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Stack
  • Practice Stack
  • MCQs on Stack
  • Stack Tutorial
  • Stack Operations
  • Stack Implementations
  • Monotonic Stack
  • Infix to Postfix
  • Prefix to Postfix
  • Prefix to Infix
  • Advantages & Disadvantages
Open In App
Next Article:
Check if given Preorder, Inorder and Postorder traversals are of same tree
Next article icon

Preorder, Postorder and Inorder Traversal of a Binary Tree using a single Stack

Last Updated : 18 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a binary tree, the task is to print all the nodes of the binary tree in Pre-order, Post-order, and In-order iteratively using only one stack traversal.

Examples:

Input:

Output:
Preorder Traversal: 1 2 3
Inorder Traversal: 2 1 3
Postorder Traversal: 2 3 1

Input:

Output:
Preorder traversal: 1 2 4 5 3 6 7
Inorder traversal: 4 2 5 1 6 3 7
Post-order traversal: 4 5 2 6 7 3 1

Approach: The problem can be solved using only one stack. The idea is to mark each node of the binary tree by assigning a value, called status code with each node such that value 1 represents that the node is currently visiting in preorder traversal, value 2 represents the nodes is currently visiting in inorder traversal and value 3 represents the node is visiting in the postorder traversal.

  • Initialize a stack < pair < Node*, int>> say S.
  • Push the root node in the stack with status as 1, i.e {root, 1}.
  • Initialize three vectors of integers say preorder, inorder, and postorder.
  • Traverse the stack until the stack is empty and check for the following conditions:
    • If the status of the top node of the stack is 1 then update the status of the top node of the stack to 2 and push the top node in the vector preorder and insert the left child of the top node if it is not NULL in the stack S.
    • If the status of the top node of the stack is 2 then update the status of the top node of the stack to 3 and push the top node in the vector inorder and insert the right child of the top node if it is not NULL in the stack S.
    • If the status of the top node of the stack is 3 then push the top node in vector postorder and then pop the top element.
  • Finally, print vectors preorder, inorder, and postorder.

Below is the implementation of the above approach:

C++
// C++ program for the above approach  #include <bits/stdc++.h> using namespace std;  // Structure of the // node of a binary tree struct Node {     int data;     struct Node *left, *right;      Node(int data)     {         this->data = data;         left = right = NULL;     } };  // Function to print all nodes of a // binary tree in Preorder, Postorder // and Inorder using only one stack void allTraversal(Node* root) {     // Stores preorder traversal     vector<int> pre;      // Stores inorder traversal     vector<int> post;      // Stores postorder traversal     vector<int> in;      // Stores the nodes and the order     // in which they are currently visited     stack<pair<Node*, int> > s;      // Push root node of the tree     // into the stack     s.push(make_pair(root, 1));      // Traverse the stack while     // the stack is not empty     while (!s.empty()) {          // Stores the top         // element of the stack         pair<Node*, int> p = s.top();          // If the status of top node         // of the stack is 1         if (p.second == 1) {              // Update the status             // of top node             s.top().second++;              // Insert the current node             // into preorder, pre[]             pre.push_back(p.first->data);              // If left child is not NULL             if (p.first->left) {                  // Insert the left subtree                 // with status code 1                 s.push(make_pair(                     p.first->left, 1));             }         }          // If the status of top node         // of the stack is 2         else if (p.second == 2) {              // Update the status             // of top node             s.top().second++;              // Insert the current node             // in inorder, in[]             in.push_back(p.first->data);              // If right child is not NULL             if (p.first->right) {                  // Insert the right subtree into                 // the stack with status code 1                 s.push(make_pair(                     p.first->right, 1));             }         }          // If the status of top node         // of the stack is 3         else {              // Push the current node             // in post[]             post.push_back(p.first->data);              // Pop the top node             s.pop();         }     }      cout << "Preorder Traversal: ";     for (int i = 0; i < pre.size(); i++) {         cout << pre[i] << " ";     }     cout << "\n";      // Printing Inorder     cout << "Inorder Traversal: ";      for (int i = 0; i < in.size(); i++) {         cout << in[i] << " ";     }     cout << "\n";      // Printing Postorder     cout << "Postorder Traversal: ";      for (int i = 0; i < post.size(); i++) {         cout << post[i] << " ";     }     cout << "\n"; }  // Driver Code int main() {      // Creating the root     struct Node* root = new Node(1);     root->left = new Node(2);     root->right = new Node(3);     root->left->left = new Node(4);     root->left->right = new Node(5);     root->right->left = new Node(6);     root->right->right = new Node(7);      // Function call     allTraversal(root);      return 0; } 
Java
// Java program for the above approach import java.util.ArrayList; import java.util.Stack;  class GFG  {      static class Pair      {         Node first;         int second;          public Pair(Node first, int second)          {             this.first = first;             this.second = second;         }     }      // Structure of the     // node of a binary tree     static class Node      {         int data;         Node left, right;          Node(int data)         {             this.data = data;             left = right = null;         }     };      // Function to print all nodes of a     // binary tree in Preorder, Postorder     // and Inorder using only one stack     static void allTraversal(Node root)     {                // Stores preorder traversal         ArrayList<Integer> pre = new ArrayList<>();          // Stores inorder traversal         ArrayList<Integer> in = new ArrayList<>();          // Stores postorder traversal         ArrayList<Integer> post = new ArrayList<>();          // Stores the nodes and the order         // in which they are currently visited         Stack<Pair> s = new Stack<>();          // Push root node of the tree         // into the stack         s.push(new Pair(root, 1));          // Traverse the stack while         // the stack is not empty         while (!s.empty())         {              // Stores the top             // element of the stack             Pair p = s.peek();              // If the status of top node             // of the stack is 1             if (p.second == 1)              {                  // Update the status                 // of top node                 s.peek().second++;                  // Insert the current node                 // into preorder, pre[]                 pre.add(p.first.data);                  // If left child is not null                 if (p.first.left != null)                  {                      // Insert the left subtree                     // with status code 1                     s.push(new Pair(p.first.left, 1));                 }             }              // If the status of top node             // of the stack is 2             else if (p.second == 2) {                  // Update the status                 // of top node                 s.peek().second++;                  // Insert the current node                 // in inorder, in[]                 in.add(p.first.data);                  // If right child is not null                 if (p.first.right != null) {                      // Insert the right subtree into                     // the stack with status code 1                     s.push(new Pair(p.first.right, 1));                 }             }              // If the status of top node             // of the stack is 3             else {                  // Push the current node                 // in post[]                 post.add(p.first.data);                  // Pop the top node                 s.pop();             }         }          System.out.print("Preorder Traversal: ");         for (int i : pre) {             System.out.print(i + " ");         }         System.out.println();          // Printing Inorder         System.out.print("Inorder Traversal: ");         for (int i : in) {             System.out.print(i + " ");         }         System.out.println();          // Printing Postorder         System.out.print("Postorder Traversal: ");         for (int i : post) {             System.out.print(i + " ");         }         System.out.println();     }      // Driver Code     public static void main(String[] args) {          // Creating the root         Node root = new Node(1);         root.left = new Node(2);         root.right = new Node(3);         root.left.left = new Node(4);         root.left.right = new Node(5);         root.right.left = new Node(6);         root.right.right = new Node(7);          // Function call         allTraversal(root);      } }      // This code is contributed by sanjeev255 
Python3
# Python3 program for the above approach  # Structure of the # node of a binary tree class Node:     def __init__(self, x):         self.data = x         self.left = None         self.right = None  # Function to print all nodes of a # binary tree in Preorder, Postorder # and Inorder using only one stack def allTraversal(root):        # Stores preorder traversal     pre = []      # Stores inorder traversal     post = []      # Stores postorder traversal     inn = []      # Stores the nodes and the order     # in which they are currently visited     s = []      # Push root node of the tree     # into the stack     s.append([root, 1])      # Traverse the stack while     # the stack is not empty     while (len(s) > 0):          # Stores the top         # element of the stack         p = s[-1]         #del s[-1]          # If the status of top node         # of the stack is 1         if (p[1] == 1):              # Update the status             # of top node             s[-1][1] += 1              # Insert the current node             # into preorder, pre[]             pre.append(p[0].data)              # If left child is not NULL             if (p[0].left):                  # Insert the left subtree                 # with status code 1                 s.append([p[0].left, 1])          # If the status of top node         # of the stack is 2         elif (p[1] == 2):              # Update the status             # of top node             s[-1][1] += 1              # Insert the current node             # in inorder, in[]             inn.append(p[0].data);              # If right child is not NULL             if (p[0].right):                  # Insert the right subtree into                 # the stack with status code 1                 s.append([p[0].right, 1])          # If the status of top node         # of the stack is 3         else:              # Push the current node             # in post[]             post.append(p[0].data);              # Pop the top node             del s[-1]      print("Preorder Traversal: ",end=" ")     for i in pre:         print(i,end=" ")     print()      # Printing Inorder     print("Inorder Traversal: ",end=" ")      for i in inn:         print(i,end=" ")     print()      # Printing Postorder     print("Postorder Traversal: ",end=" ")      for i in post:         print(i,end=" ")     print()   # Driver Code if __name__ == '__main__':      # Creating the root     root = Node(1)     root.left = Node(2)     root.right = Node(3)     root.left.left = Node(4)     root.left.right = Node(5)     root.right.left = Node(6)     root.right.right = Node(7)      # Function call     allTraversal(root)      # This code is contributed by mohit kumar 29. 
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG {          // Class containing left and     // right child of current     // node and key value     class Node {                 public int data;         public Node left, right;                 public Node(int x)         {             data = x;             left = right = null;         }     }          // Function to print all nodes of a     // binary tree in Preorder, Postorder     // and Inorder using only one stack     static void allTraversal(Node root)     {                 // Stores preorder traversal         List<int> pre = new List<int>();           // Stores inorder traversal         List<int> In = new List<int>();           // Stores postorder traversal         List<int> post = new List<int>();           // Stores the nodes and the order         // in which they are currently visited         Stack<Tuple<Node, int>> s = new Stack<Tuple<Node, int>>();           // Push root node of the tree         // into the stack         s.Push(new Tuple<Node, int>(root, 1));           // Traverse the stack while         // the stack is not empty         while (s.Count > 0)         {               // Stores the top             // element of the stack             Tuple<Node, int> p = s.Peek();               // If the status of top node             // of the stack is 1             if (p.Item2 == 1)             {                   // Update the status                 // of top node                 Tuple<Node, int> temp = s.Peek();                 temp = new Tuple<Node, int>(temp.Item1, temp.Item2 + 1);                 s.Pop();                 s.Push(temp);                   // Insert the current node                 // into preorder, pre[]                 pre.Add(p.Item1.data);                   // If left child is not null                 if (p.Item1.left != null)                 {                       // Insert the left subtree                     // with status code 1                     s.Push(new Tuple<Node, int>(p.Item1.left, 1));                 }             }               // If the status of top node             // of the stack is 2             else if (p.Item2 == 2) {                   // Update the status                 // of top node                 Tuple<Node, int> temp = s.Peek();                 temp = new Tuple<Node, int>(temp.Item1, temp.Item2 + 1);                 s.Pop();                 s.Push(temp);                   // Insert the current node                 // in inorder, in[]                 In.Add(p.Item1.data);                   // If right child is not null                 if (p.Item1.right != null) {                       // Insert the right subtree into                     // the stack with status code 1                     s.Push(new Tuple<Node, int>(p.Item1.right, 1));                 }             }               // If the status of top node             // of the stack is 3             else {                   // Push the current node                 // in post[]                 post.Add(p.Item1.data);                   // Pop the top node                 s.Pop();             }         }           Console.Write("Preorder Traversal: ");         foreach(int i in pre) {             Console.Write(i + " ");         }         Console.WriteLine();           // Printing Inorder         Console.Write("Inorder Traversal: ");         foreach(int i in In) {             Console.Write(i + " ");         }         Console.WriteLine();           // Printing Postorder         Console.Write("Postorder Traversal: ");         foreach(int i in post) {             Console.Write(i + " ");         }         Console.WriteLine();     }        static void Main() {     // Creating the root     Node root = new Node(1);     root.left = new Node(2);     root.right = new Node(3);     root.left.left = new Node(4);     root.left.right = new Node(5);     root.right.left = new Node(6);     root.right.right = new Node(7);      // Function call     allTraversal(root);   } }  // This code is contributed by suresh07. 
JavaScript
<script>  // Javascript program for the above approach class Pair {     constructor(first, second)     {         this.first = first;         this.second = second;     } }  // Structure of the // node of a binary tree class Node {     constructor(data)     {         this.data = data;         this.left = this.right = null;     } }  // Function to print all nodes of a // binary tree in Preorder, Postorder // and Inorder using only one stack function allTraversal(root) {          // Stores preorder traversal     let pre = [];      // Stores inorder traversal     let In = [];      // Stores postorder traversal     let post = [];      // Stores the nodes and the order     // in which they are currently visited     let s = [];      // Push root node of the tree     // into the stack     s.push(new Pair(root, 1));      // Traverse the stack while     // the stack is not empty     while (s.length != 0)     {                  // Stores the top         // element of the stack         let p = s[s.length - 1];          // If the status of top node         // of the stack is 1         if (p.second == 1)         {              // Update the status             // of top node             s[s.length - 1].second++;              // Insert the current node             // into preorder, pre[]             pre.push(p.first.data);              // If left child is not null             if (p.first.left != null)             {                                  // Insert the left subtree                 // with status code 1                 s.push(new Pair(p.first.left, 1));             }         }          // If the status of top node         // of the stack is 2         else if (p.second == 2)         {                          // Update the status             // of top node             s[s.length - 1].second++;              // Insert the current node             // in inorder, in[]             In.push(p.first.data);              // If right child is not null             if (p.first.right != null)             {                                  // Insert the right subtree into                 // the stack with status code 1                 s.push(new Pair(p.first.right, 1));             }         }          // If the status of top node         // of the stack is 3         else          {                          // Push the current node             // in post[]             post.push(p.first.data);              // Pop the top node             s.pop();         }     }      document.write("Preorder Traversal: ");     for(let i = 0; i < pre.length; i++)      {         document.write(pre[i] + " ");     }     document.write("<br>");      // Printing Inorder     document.write("Inorder Traversal: ");     for(let i = 0; i < In.length; i++)     {         document.write(In[i] + " ");     }     document.write("<br>");      // Printing Postorder     document.write("Postorder Traversal: ");     for(let i = 0; i < post.length; i++)     {         document.write(post[i] + " ");     }     document.write("<br>"); }  // Driver Code  // Creating the root let root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6); root.right.right = new Node(7);  // Function call allTraversal(root);  // This code is contributed by unknown2108  </script> 

Output: 
Preorder Traversal: 1 2 4 5 3 6 7  Inorder Traversal: 4 2 5 1 6 3 7  Postorder Traversal: 4 5 2 6 7 3 1

 

Time Complexity: O(N)
Auxiliary Space: O(N)


Next Article
Check if given Preorder, Inorder and Postorder traversals are of same tree

D

deepika_sharma
Improve
Article Tags :
  • Tree
  • Stack
  • Algorithms
  • DSA
  • Inorder Traversal
  • Preorder Traversal
  • PostOrder Traversal
  • Binary Tree
  • Tree Traversals
  • cpp-stack
Practice Tags :
  • Algorithms
  • Stack
  • Tree

Similar Reads

  • Postorder traversal of Binary Tree without recursion and without stack
    Given a binary tree, perform postorder traversal. Prerequisite - Inorder/preorder/postorder traversal of tree We have discussed the below methods for postorder traversal. 1) Recursive Postorder Traversal. 2) Postorder traversal using Stack. 2) Postorder traversal using two Stacks. Approach 1 The app
    15 min read
  • Pre Order, Post Order and In Order traversal of a Binary Tree in one traversal | (Using recursion)
    Given a binary tree, the task is to print all the nodes of the binary tree in Pre-order, Post-order, and In-order in one iteration. Examples: Input: Output: Pre Order: 1 2 4 5 3 6 7 Post Order: 4 5 2 6 7 3 1 In Order: 4 2 5 1 6 3 7 Input: Output: Pre Order: 1 2 4 8 12 5 9 3 6 7 10 11 Post Order: 12
    9 min read
  • Check if given Preorder, Inorder and Postorder traversals are of same tree
    Given the Preorder, Inorder, and Postorder traversals of some trees, the task is to check if they all belong to the same tree. Examples: Input: inOrder[] = [4, 2, 5, 1, 3] preOrder[] = [1, 2, 4, 5, 3] postOrder = [4, 5, 2, 3, 1] Output: TrueExplanation: All of the above three traversals are of the s
    15+ min read
  • Binary Search Tree (BST) Traversals – Inorder, Preorder, Post Order
    Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree.  Input:  Output: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 100 Input:  Output:
    11 min read
  • Post Order Traversal of Binary Tree in O(N) using O(1) space
    Prerequisites:- Morris Inorder Traversal, Tree Traversals (Inorder, Preorder and Postorder)Given a Binary Tree, the task is to print the elements in post order using O(N) time complexity and constant space. Input: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ 8 9Output: 8 9 4 5 2 6 7 3 1Input: 5 / \ 7 3 / \ / \ 4 1
    15+ min read
  • Modify a binary tree to get preorder traversal using right pointers only
    Given a binary tree. The task is to modify it in such a way that after modification preorder traversal of it can get only with the right pointers. During modification, we can use right as well as left pointers. Examples: Input : Output : Explanation: The preorder traversal of given binary tree is 10
    12 min read
  • Print Postorder traversal from given Inorder and Preorder traversals
    Given two arrays represent Inorder and Preorder traversals of a binary tree, the task is to find the Postorder traversal. Example: Input: in[] = [4, 2, 5, 1, 3, 6] pre[] = [1, 2, 4, 5, 3, 6]Output: 4 5 2 6 3 1Explanation: Traversals in the above example represents following tree: A naive method is t
    11 min read
  • Preorder from Inorder and Postorder traversals
    Given the Inorder and Postorder traversals of a binary tree, the task is to find the Preorder traversal. Examples: Input: post[] = [4, 5, 2, 6, 3, 1] in[] = [4, 2, 5, 1, 3, 6]Output: 1 2 4 5 3 6Explanation: Traversals in the above example represents following tree: The idea is to first construct the
    14 min read
  • Construct Full Binary Tree from given preorder and postorder traversals
    Given two arrays that represent preorder and postorder traversals of a full binary tree, construct the binary tree. Full Binary Tree is a binary tree where every node has either 0 or 2 children. Examples of Full Trees. Input: pre[] = [1, 2, 4, 8, 9, 5, 3, 6, 7] , post[] = [8, 9, 4, 5, 2, 6, 7, 3, 1]
    9 min read
  • Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
    We have discussed Thread based Morris Traversal. Can we do inorder traversal without threads if we have parent pointers available to us? Input: Root of Below Tree [Every node of tree has parent pointer also] 10 / \ 5 100 / \ 80 120 Output: 5 10 80 100 120 The code should not extra space (No Recursio
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences