Post-Order Tree Traversal using Python

Post-order tree traversal is basically traversing the Left, Right, and Root node of each sub-tree. We can understand it better by using below algorithm:

  • Traverse the left subtree by recursively calling the postorder traversal function.
  • Traverse the right subtree by recursively calling the postorder traversal function.
  • Visit the root node.

Example:

The provided code showcases the postorder traversal method for a binary tree. The `Node` class represents a node in the tree, characterized by its data, left child, and right child. The `printPostOrder` function conducts a recursive postorder traversal on the tree. In this traversal, the left subtree is traversed first, followed by the right subtree, and then the current node’s data is visited and printed. This sequence ensures that a node is processed after both its left and right subtrees have been traversed (hence, “postorder”). The driver code constructs a sample binary tree and subsequently calls the `printPostOrder` function to display the result of the postorder traversal of the tree. The nodes’ values will be printed in the sequence dictated by the postorder traversal technique.

Python3




class Node:
    def __init__(self, v):
        self.data = v
        self.left = None
        self.right = None
 
# Preorder Traversal
def printPostOrder(node):
    if node is None:
        return
 
    # Traverse left subtree
    printPostOrder(node.left)
 
    # Traverse right subtree
    printPostOrder(node.right)
     
    # Visit Node
    print(node.data, end = " ")
 
# Driver code
if __name__ == "__main__":
    # Build the tree
    root = Node(10)
    root.left = Node(2)
    root.right = Node(20)
    root.left.left = Node(1)
    root.left.right = Node(3)
    root.right.left = Node(15)
    root.right.right = Node(30)
 
    # Function call
    print("Postorder Traversal: ", end = "")
    printPostOrder(root)


Output

Postorder Traversal: 1 3 2 15 30 20 10 




Tree Traversal Techniques in Python

A tree is a non-linear data structure or we can say that it is a type of hierarchical structure. It is like an inverted tree where roots are present at the top and leaf or child nodes are present at the bottom of the tree. The root node and its child nodes are connected through the edges like the branches in the tree. In this article, we will learn different ways of traversing a tree in Python.

Prerequisites for Tree Traversal in Python

Similar Reads

Different Types of Tree Traversal Techniques

There are three types of tree traversal techniques:...

Overview of Tree Traversal Techniques

In the below image we can see a binary tree and the result of each traversal technique. We will be going to understand each technique in detail....

Inorder Tree Traversal in Python

Inorder tree traversal is basically traversing the left, root, and right node of each sub-tree. We can understand it better by using below algorithm:...

Pre-Order Tree Traversal in Python

...

Post-Order Tree Traversal using Python

Pre-order tree traversal is basically traversing the Root, Left, and Right node of each sub-tree. We can understand it better by using below algorithm:...

Contact Us