1) Create an empty stack S. 2) Initialize current node as root 3) Push the current node to S and set current = current->left until current is NULL 4) If current is NULL and stack is not empty then a) Pop the top item from stack. b) Print the popped item, set current = popped_item->right c) Go to step 3. 5) If current is NULL and stack is empty then we are done.
Let us consider the below tree for example
1 / \ 2 3 / \ 4 5 Step 1 Creates an empty stack: S = NULL Step 2 sets current as address of root: current -> 1 Step 3 Pushes the current node and set current = current->left until current is NULL current -> 1 push 1: Stack S -> 1 current -> 2 push 2: Stack S -> 2, 1 current -> 4 push 4: Stack S -> 4, 2, 1 current = NULL Step 4 pops from S a) Pop 4: Stack S -> 2, 1 b) print "4" c) current = NULL /*right of 4 */ and go to step 3 Since current is NULL step 3 doesn't do anything. Step 4 pops again. a) Pop 2: Stack S -> 1 b) print "2" c) current -> 5/*right of 2 */ and go to step 3 Step 3 pushes 5 to stack and makes current NULL Stack S -> 5, 1 current = NULL Step 4 pops from S a) Pop 5: Stack S -> 1 b) print "5" c) current = NULL /*right of 5 */ and go to step 3 Since current is NULL step 3 doesn't do anything Step 4 pops again. a) Pop 1: Stack S -> NULL b) print "1" c) current -> 3 /*right of 1 */ Step 3 pushes 3 to stack and makes current NULL Stack S -> 3 current = NULL Step 4 pops from S a) Pop 3: Stack S -> NULL b) print "3" c) current = NULL /*right of 3 */ Traversal is done now as stack S is empty and current is NULL.
4 2 5 1 3
Time Complexity: O(n)
See this post for another approach of Inorder Tree Traversal without recursion and without stack!
Please write comments if you find any bug in above code/algorithm, or want to share more information about stack based Inorder Tree Traversal.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Inorder Tree Traversal without recursion and without stack!
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Postorder traversal of Binary Tree without recursion and without stack
- Cartesian tree from inorder traversal | Segment Tree
- Preorder Traversal of N-ary Tree Without Recursion
- Construct Special Binary Tree from given Inorder traversal
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Inorder traversal of an N-ary Tree
- Check if Inorder traversal of a Binary Tree is palindrome or not
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Print Postorder traversal from given Inorder and Preorder traversals
- Find all possible binary trees with given Inorder Traversal
- Find n-th node of inorder traversal
- DFS traversal of a tree using recursion
- Zig-Zag traversal of a Binary Tree using Recursion
- Level order traversal of Binary Tree using Morris Traversal
- Print ancestors of a given binary tree node without recursion
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Tree Traversals (Inorder, Preorder and Postorder)
- Construct Tree from given Inorder and Preorder traversals