Inorder Traversal
Learn more about inorder traversal and how to code it iteratively and recursively.
We'll cover the following...
Iterative inorder tree traversal
Inorder tree traversal is very useful and quickly finds the tree’s balance. When we use inorder tree traversal, we start by recursively visiting the left subtree first. After that, we visit the root and then the right subtree. Each node stores a value, and we know this value as the key. In inorder tree traversal, the output produces sorted key values in ascending order.
First, let’s look at inorder tree traversal using iteration.
As mentioned earlier, the output is presented in ascending order.
A B C D E F
In inorder traversal, it pushes the root node first, followed by its left node.
-
Line 23: We make the left node
current, and we continue it until we encounter a leaf. -
Lines 25–27: Once we reach the leaf, we print it (mark it as traversed), followed by its parent and right node, and again continue traversing until ...