# Postorder Traversal

In this article, we will discuss the postorder traversal in data structure.

Linear data structures such as stack, array, queue, etc., only have one way to traverse the data. But in a hierarchical data structure such as tree, there are multiple ways to traverse the data. So, here we will discuss another way to traverse the tree data structure, i.e., postorder traversal. The postorder traversal is one of the traversing techniques used for visiting the node in the tree. It follows the principle LRN (Left-right-node). Postorder traversal is used to get the postfix expression of a tree.

The following steps are used to perform the postorder traversal:

• Traverse the left subtree by calling the postorder function recursively.
• Traverse the right subtree by calling the postorder function recursively.
• Access the data part of the current node.

The post order traversal technique follows the Left Right Root policy. Here, Left Right Root means the left subtree of the root node is traversed first, then the right subtree, and finally, the root node is traversed. Here, the Postorder name itself suggests that the tree's root node would be traversed at last.

### Algorithm

Now, let's see the algorithm of postorder traversal.

### Example of postorder traversal

Now, let's see an example of postorder traversal. It will be easier to understand the process of postorder traversal using an example.

The nodes with yellow color are not visited yet. Now, we will traverse the nodes of above tree using postorder traversal.

• Here, 40 is the root node. We first visit the left subtree of 40, i.e., 30. Node 30 will also traverse in post order. 25 is the left subtree of 30, so it is also traversed in post order. Then 15 is the left subtree of 25. But 15 has no subtree, so print 15 and move towards the right subtree of 25.
• 28 is the right subtree of 25, and it has no children. So, print 28.
• Now, print 25, and the postorder traversal for 25 is finished.
• Next, move towards the right subtree of 30. 35 is the right subtree of 30, and it has no children. So, print 35.
• After that, print 30, and the postorder traversal for 30 is finished. So, the left subtree of given binary tree is traversed.
• Now, move towards the right subtree of 40 that is 50, and it will also traverse in post order. 45 is the left subtree of 50, and it has no children. So, print 45 and move towards the right subtree of 50.
• 60 is the right subtree of 50, which will also be traversed in post order. 55 is the left subtree of 60 that has no children. So, print 55.
• Now, print 70, which is the right subtree of 60.
• Now, print 60, and the post order traversal for 60 is completed.
• Now, print 50, and the post order traversal for 50 is completed.
• At last, print 40, which is the root node of the given binary tree, and the post order traversal for node 40 is completed.

The final output that we will get after postorder traversal is -

{15, 28, 25, 35, 30, 45, 55, 70, 60, 50, 40}

## Complexity of Postorder traversal

The time complexity of postorder traversal is O(n), where 'n' is the size of binary tree.

Whereas, the space complexity of postorder traversal is O(1), if we do not consider the stack size for function calls. Otherwise, the space complexity of postorder traversal is O(h), where 'h' is the height of tree.

## Implementation of Postorder traversal

Now, let's see the implementation of postorder traversal in different programming languages.

Program: Write a program to implement postorder traversal in C language.

Output

Program: Write a program to implement postorder traversal in C++.

Output

Program: Write a program to implement postorder traversal in C#.

Output

After the execution of the above code, the output will be -

Program: Write a program to implement postorder traversal in Java.

Output

After the execution of the above code, the output will be -

So, that's all about the article. Hope the article will be helpful and informative to you.

Next TopicSparse Matrix