What is Morris traversal?

Morris (InOrder) traversal is a tree traversal algorithm that does not employ the use of recursion or a stack. In this traversal, links are created as successors and nodes are printed using these links. Finally, the changes are reverted back to restore the original tree.

Algorithm

  • Initialize the root as the current node curr.

  • While curr is not NULL, check if curr has a left child.

  • If curr does not have a left child, print curr and update it to point to the node on the right of curr.

  • Else, make curr the right child of the rightmost node in curr's left subtree.

  • Update curr to this left node.

Demo

Let’s take the binary tree given below and traverse it using Morris (InOrder) traversal.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

44 is the root, so it is initialized as curr. 44 has a left child, so it is made the rightmost right child of it’s left subtree (the immediate predecessor to 44 in an InOrder traversal). Finally, 44 is made the right child of 33 and curr is set to 22.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Now the curr is on 22 and has a left child and right child is already linked to root. So we move the curr to 11. curr has no left and right child. Right will be linked to the root 22

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

11 is printed because it has no left child and curr is returned to 22, which was made to be 11's right child in the previous iteration. On the next iteration, 22 has both children. However, the dual-condition of the loop makes it stop when it reaches itself; this is an indication that its left subtree has already been traversed. So, it prints itself and continues with its right subtree 33. 33 prints itself, and curr becomes 33 and goes through the same checking process that 22 did. It also realizes that its left subtree has been traversed and continues with the 44. The rest of the tree follows this same pattern.

Code

The above algorithm is implemented in the code below:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved