Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Postorder Traversal

Postorder traversal is used to visit the node in the tree. It pursues the rule of LRN, which means Left-right-node. Basically, this traversal helps in getting the postfix phrase of a tree.

Table of Contents

Postorder Traversal Steps

In the postorder traverse, firstly, we traverse the left subelement of the root node, then we move towards the right subelement and eventually move to the root node. This is how the process of traversing occurs here.

Read more about tree traversal to get a better understanding of this topic.

Algorithm for Postorder Traversal

Let’s now examine the postorder traversal algorithm.

For all nodes of the tree:

Step 1: Firstly, we traverse the left subelement repeatedly.

Step 2: Now, in the second stage, we traverse the right subelement repeatedly.

Step 3: Visit the root node.

Demonstration of PT

Now, let’s start with the example of postorder traversal.

Postorder Traversal 1

None of the nodes has been seen yet. Now, we need to traverse the tree by employing the techniques of postorder.

  • In the above structure, 39 is reflected as the root node. We begin with the left element of 39, which is 29. In addition, Node 29 will travel in post order. Because 24 is the left subelement of 29, it is explored in post order. Now we’ll go on to another segment which is 14. The left subelement of 24 is 14. However, as we can see, there is no subelement for 14; therefore, we can publish 14 and go ahead with the right subelement of 24.

    Postorder Traversal 2

The right subelement of 24 is 27, and we can see that it doesn’t have any children. So, we will print 27.

Postorder Traversal 3

  • Now, print 24.

    Postorder Traversal 4
  • Proceed further with the right subelement of 29. 34 is the right subelement of 29, and it has no children. So, we can print 34.

Postorder Traversal 5

  • Now, publish 29, part 29 is completed here.

Postorder Traversal 6

  • The left subelement has traversed. Now, we can proceed further with the right subelement of 39, which is 49, and it will also move in post order. 44 is the left subelement of 49, and we can identify that it has no subelements(child). So, publish 44 and proceed with the right subelement of 49.

Postorder Traversal 7

  • The subelement of 49 is 59. 54 is the left subelement of 59, and it has no subelement (child). So, publish 54.

Postorder Traversal 8

  • Now, print 69, and it is the right subelement of 59.

Postorder Traversal 9

  • Print 59 and the traverse are completed here.

Postorder Traversal 10

  • Now, we will display 49, and the post order traversal for 49 is finished.

Postorder Traversal 11

  • In the end, print 39, the root node of the provided binary tree.

Postorder Traversal 13

The conclusion is:

{14,27,26,29,44,54,69,59,49,39}

Time Complexity

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