traversal using stack
traversal using stack
In this shot, we use two stacks to traverse the binary tree in post-order.
Algorithm
1.
2. Inorder traversal of a binary tree
An iterative inorder traversal of a binary tree is done using the stack data structure.
Algorithm
1. Visit a node.
2. Visit the left child of the node.
3. Visit the right child of the node.
Algorithm
The right child is added to the stack before the left child so that we can pop the left element first, since the
stack is a Last-in-First-Out (LIFO) data structure, and in a preorder traversal, the left child should be
traversed before the right child.