VOOZH about

URL: https://www.geeksforgeeks.org/dsa/postorder-traversal-binary-tree-without-recursion-without-stack/

⇱ Postorder traversal of Binary Tree without recursion and without stack - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Postorder traversal of Binary Tree without recursion and without stack

Last Updated : 31 Mar, 2023

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 approach used is based on using an unordered set to keep track of visited nodes and a while loop to traverse the tree. The steps involved in the approach can be expressed mathematically as follows:

  • Initialize a pointer temp to the root node of the binary tree.
  • Initialize an empty unordered set visited to keep track of visited nodes.
  • While temp is not null and temp is not already visited (i.e., temp is not in visited set):
    a. If temp has a left child and the left child is not already visited, set temp to the left child.
    b. Else if temp has a right child and the right child is not already visited, set temp to the right child.
    c. Else, print the data of the current node temp, add temp to visited set, and set temp to the root node.
  • Return from the function.
     

Algorithm

Define a struct Node with integer data, pointer to left child and pointer to right child.
Define a helper function called "postorder" which takes a pointer to the head of the tree.
Create a pointer "temp" and an unordered set "visited".
While "temp" is not NULL and "temp" is not visited before:
 a. If "temp" has a left child and the left child is not visited before, then set "temp" to its left child and continue the loop.
 b. If "temp" does not have a left child or the left child is already visited, check if "temp" has a right child and the right child is not visited before. If yes, set "temp" to its right child and continue the loop.
 c. If "temp" does not have a left child or the left child is already visited, and "temp" does not have a right child or the right child is already visited, then print the data of "temp", insert "temp" into "visited" set, and set "temp" to the head of the tree.
Define a function called "newNode" which takes an integer data as input and returns a new Node with the given data, NULL left pointer, and NULL right pointer.
 

Output:  

1 4 7 6 3 13 14 10 8 

Time complexity: O(N) where N is no of nodes in a binary tree

Auxiliary Space: O(n) since using unordered_set

Alternate Solution: 

We can keep the visited flag with every node instead of a separate hash table. 

Output:  

1 4 7 6 3 13 14 10 8 

Time complexity: O(n2) in worst case we move pointer back to head after visiting every node.

Auxiliary Space: O(1) 

Alternate solution using unordered_map in which we do not have to move pointer back to head, so time complexity is O(n).

Output: 

1 4 7 6 3 13 14 10 8 

Time complexity: O(n) where n is no of nodes in a binary tree

Auxiliary Space: O(n) since using unordered_map

Comment
Article Tags: