Postorder traversal

Postorder traversal: In postorder traversal root is visited at the last.

Steps:

1. Traverse the left subtree
2. Traverse the right subtree
3. Visit the root

Code:

void postOrder(Node root) {
    if(root==null)
        return;
    postOrder(root.left);
    postOrder(root.right);
    System.out.print(root.data+" ");

}

Illustration:

Postorder traversal of given tree is 2, 4, 5, 3, 1

Traverse the left subtree
 Traverse the right subtree
 Traverse the left subtree
 Traverse the right subtree
 Visit the root
 Visit the root ( since we finished traversing left and right subtrees of 7 )



Comments

Popular posts from this blog

Tree traversals - Inorder Traversal

Binary Search tree (Insertion)

Stack implementation using array