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:
Traverse the left subtree
Traverse the right subtree
Visit the root
Visit the root ( since we finished traversing left and right subtrees of 7 )
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 subtreeTraverse the left subtree
Traverse the right subtree
Visit the root
Visit the root ( since we finished traversing left and right subtrees of 7 )







Comments
Post a Comment