Tree is one of the most outright hierarchical data structure available. And always remembering the traversal has been a little confusing (At least for me). Root first or Left first or Right First?
While people can remember easily like say start with Root,Left,Right and then move Root step right fir every order it will be easy. Say below.
Pre-Order – Root, Left, Right
In-Order – Left, Root, Right
Post-Order – Left, Right, Root
While the above is easy to comprehend when given a tree we get a little confused. Below is a diagram that will explain you to remember easily. As soon as u see the tree you can write the sequence right away.
Above image is self explanatory, still I would comment a little.
Pre-Order – Keep marking the arrow from Root then extreme left and then right. Unfortunately I cannot think of a key word that you can associate but the simple jump from one node to other in that fashion you can quickly write it.
In-Order: Remember the free fall, mark arrows downwards, all tree leafs are falling. Then you print in-order. Just remember this is a very important order widely used. Especially in BST where in order to print a number in sorted order. We use In order.
Post-Order: Leaf break or Leaf cutting, start removing the leaf one by one in the sequence of Left then Right and then finally Root you get the post order sequence.
You may check this in video as well, but that’s in a Regional language Hindi.