Search code examples
data-structuresbinary-treecomputer-sciencerecursive-datastructures

How to remember in-order traversal and pre-order traversal in an easy and unforgettable way?


Binary Tree traversal such as preorder traversal, inorder traversal, postorder traversal, and level order traversal is commonly interviewed by many IT companies.

It confuses me to remember the iterative implementation of pre-order traversal and inorder traversal.

Here are the problems from leetcode.

https://leetcode.com/problems/binary-tree-inorder-traversal/

https://leetcode.com/problems/binary-tree-preorder-traversal/


Solution

    1. preorder traversal:

    We walk the graph, from top going counter-clockwise. Shout every time we pass the LEFT of a node. enter image description here

    1. inorder traversal:

    We walk the graph, from top going counter-clockwise. Shout every time Shout when you cross the bottom. enter image description here

    1. post-order traversal

    We walk the graph, from top going counter-clockwise. Shout every time Shout when you cross the right enter image description here

    If you want to see more recursive and iterative implementation details, please read the following post