Inorder Traversal: A binary tree can be traversed into 4 major ways and they are:

  • Inorder Traversal
  • Pre Order Traversal
  • Post Order Traversal
  • level first Traversal

We have already discussed level first traversal of a Binary search tree. In this tutorial, we will discuss the in-order traversal of a binary tree.

Inorder Traversal

In the inorder traversal technique, we first visit the left child node, then the root/parent node and then the right child node. Take a look at the picture below with three nodes and their inorder traversal output.

inorder traversal

Inorder Traversal Algorithm

The algorithm for inorder traversal is very simple an same as traversing the 3 nodes as stated above.

  1. Select the root of the tree.
  2. Check if it has any left child nodes. If yes then repeat this step (Step 2). Else go to step 3
  3. Print/output the node data.
  4. Check if it has any right child. If yes then repeat step 2 for the current node. Else print the node data.
  5. End.

inorder traversal in binary tree

Inorder Traversal code




inorder traversal in c++

Notice the output of this tree traversal c++ program. It is in ascending order. It is because In-order traversal in the binary tree gives output from the minimum value to the maximum value.

Also in the code, we have inserted the node into such a way that it forms binary search tree (A node will have only 2 child nodes, left child node with smaller value and right child node with greater value).

That’s all for the inorder traversal of a binary tree in C++ and Java. If you have any doubt then comment below.

Leave a Reply