Binary tree inorder traversal without recursion
We have seen inorder traversal algorithm for a binary tree, but it was a recursive version. In this article, we'll take a look at the non-recursive approach
The inorder binary tree traversal algorithm can be described:
- Visit the
left subtree - Visit the
root - Visit the
right-subtree

During inorder traversal, nodes will be printed in the following order: 5, 4, 7, 2, 3, 8. The animated image displays the inorder traversal algorithm.

Algorithm
To implement inorder traversal without recursive we will use Stack to store the traversal
- Step 1: Create an empty
stack - Step 2: Set
currentnode asroot - Step 3: Push the
currentnode to thestackand setcurrent = current.Leftuntil thecurrentisnull - Step 4: If the
currentisnulland thestackis not empty then:- Pop a
nodefrom thestack - Print the
poppedNode, setcurrent = poppedNode.Right - Go to step
3
- Pop a
- Step 5: If the
currentisnullandstackis empty then the traversal completed
let's see step by step how the algorithm works on the example

Step 1: create an emptystackStep 2: setcurrentnode asrootStep 3: push thecurrentnode to thestackand setcurrent = current.Leftuntil thecurrentisnullcurrent = 2- push
2to thestack(Stack:2) current = 4- push
4to thestack(Stack:4, 2) current = 5- push
5to thestack(Stack:5, 4, 2) current = null
Step 4: pop5from thestack- pop
5from thestack(Stack:4, 2) - print
5 current = null. node5is a leaf it doesn't have a right child- goto
Step 3
- pop
Step 3: do nothing, becausecurrentequalsnullStep 4: pop4from thestack- pop
4from thestack(Stack:2) - print
4(Output:5, 4) current = 7- goto
Step 3
- pop
Step 3: push7to thestack- push
7to thestack(Stack:7, 2) current = null, node7is a leaf it doesn't have a left child- goto
Step 4
- push
Step 4: pop7from thestack- pop
7from thestack(Stack:2) - print
7(Output:5, 4, 7) current = null, node7is a leaf it doesn't have a right child- goto
Step 3
- pop
Step 3: do nothing, becausecurrentequalsnullStep 4: pop2from thestack- pop
2from thestack(Stack: empty) - print
2(Output:5, 4, 7, 2) current = 3- goto
Step 3
- pop
Step 3: push3to thestack- push
3to thestack(Stack:3) current = null,3doesn't have a left node- goto
Step 4
- push
Step 4: pop3from thestack- pop
3from thestack(Stack: empty) - print
3(Output:5, 4, 7, 2, 3) current = 8- goto
Step 3
- pop
Step 3: push8to thestack- push
8to thestack(Stack:8) current = null,8doesn't have a left node- goto
Step 4
- push
Step 4: pop8from thestack- pop
8from thestack(Stack: empty) - print
8(Output:5, 4, 7, 2, 3, 8) current = null
- pop
the traversal is completed: current equals null and the stack is empty
Code
public void InorderTraversal(TreeNode root)
{
if (root == null)
{
return;
}
var stack = new Stack<TreeNode>();
//TreeNode is a reference type, need this to do not break the tree.
TreeNode current = root;
while (current != null || stack.Count != 0)
{
while (current != null)
{
stack.Push(current);
current = current.Left;
}
TreeNode poppedNode = stack.Pop();
//print value
Console.WriteLine(poppedNode.Value);
current = poppedNode.Right;
}
}
Time Complexity: O(n)
Video Lesson
Recent articles
Nice to read