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
current
node asroot
- Step 3: Push the
current
node to thestack
and setcurrent = current.Left
until thecurrent
isnull
- Step 4: If the
current
isnull
and thestack
is not empty then:- Pop a
node
from thestack
- Print the
poppedNode
, setcurrent = poppedNode.Right
- Go to step
3
- Pop a
- Step 5: If the
current
isnull
andstack
is empty then the traversal completed
let's see step by step how the algorithm works on the example
Step 1
: create an emptystack
Step 2
: setcurrent
node asroot
Step 3
: push thecurrent
node to thestack
and setcurrent = current.Left
until thecurrent
isnull
current = 2
- push
2
to thestack
(Stack:2
) current = 4
- push
4
to thestack
(Stack:4, 2
) current = 5
- push
5
to thestack
(Stack:5, 4, 2
) current = null
Step 4
: pop5
from thestack
- pop
5
from thestack
(Stack:4, 2
) - print
5
current = null
. node5
is a leaf it doesn't have a right child- goto
Step 3
- pop
Step 3
: do nothing, becausecurrent
equalsnull
Step 4
: pop4
from thestack
- pop
4
from thestack
(Stack:2
) - print
4
(Output:5, 4
) current = 7
- goto
Step 3
- pop
Step 3
: push7
to thestack
- push
7
to thestack
(Stack:7, 2
) current = null
, node7
is a leaf it doesn't have a left child- goto
Step 4
- push
Step 4
: pop7
from thestack
- pop
7
from thestack
(Stack:2
) - print
7
(Output:5, 4, 7
) current = null
, node7
is a leaf it doesn't have a right child- goto
Step 3
- pop
Step 3
: do nothing, becausecurrent
equalsnull
Step 4
: pop2
from thestack
- pop
2
from thestack
(Stack: empty) - print
2
(Output:5, 4, 7, 2
) current = 3
- goto
Step 3
- pop
Step 3
: push3
to thestack
- push
3
to thestack
(Stack:3
) current = null
,3
doesn't have a left node- goto
Step 4
- push
Step 4
: pop3
from thestack
- pop
3
from thestack
(Stack: empty) - print
3
(Output:5, 4, 7, 2, 3
) current = 8
- goto
Step 3
- pop
Step 3
: push8
to thestack
- push
8
to thestack
(Stack:8
) current = null
,8
doesn't have a left node- goto
Step 4
- push
Step 4
: pop8
from thestack
- pop
8
from 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