Remember the Four Different Binary Tree Traversal for Next Code Interview
By: Jerry An
Tags: Binary Tree, Coding Interviews, Computer Science, Data Structures, Leetcode

Remember the Four Different Binary Tree Traversal for Next Code Interview Jerry An Follow Dec 23 · 3 min read

Binary Tree traversal such as preorder traversal, inorder traversal, postorder traversal, and level order traversal is commonly interviewed by many IT companies. So why not conquer the monster only in this post?

Preorder Traversal

Preorder means “Visit” a node first, then traverse its children.

Video of pre-order traversal

Procedure: Root > Left > Right

Step 1: “ Visit” the node

the node Step 2: Traverse left

Step 3: Traverse right

pre-order traversal remember plot

Remember trick:

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

Recursive implementation

To convert an inherently recursive procedure to an iterative one, we need an explicit stack. Iterative implementation as following:

Note:

If you have more than one recursive call inside and you want to preserve the order of the calls, you have to add them in the reverse order to the stack. Here, node.right is pushed into the stack before the node.left , which is different from the Recursive implementation.

to the stack. Here, is pushed into the stack before the , which is different from the Recursive implementation. stack is always occupied until the end of execution

Use Case

Example: Preorder Traversal for printing directory listing:

Inorder Traversal

Inorder traversal means Traverse left child, visit the node, then traverse right child. This is the most important traversal order which is widely used. Especially in Binary Search Tree where it prints a number in sorted order.

Video of in-order traversal

Procedure: Left >Root> Right

Step 1: Traverse left

Step 2:“ Visit” the node

the node Step 3: Traverse right

in-order traversal remember plot

Remember trick:

a. We walk the graph, from top going counter-clockwise. Shout every time Shout when you cross the bottom. b. Imagine free falling of the node

Recursive implementation:

Iterative implementation:

Postorder Traversal

Postorder means traverse its children first, then “visiting” the node.

Video of post-order traversal

Procedure: Left > Right >Root

Step 1: Traverse left

Step 2: Traverse right

Step 3: “Visit” the node

post-order traversal remember plot

Remember trick:

a. We walk the graph, from top going counter-clockwise. Shout every time Shout when you cross the right b. Imagine we are doing leaf-cutting one by one from left to right, then finally the root.

Recursive implementation:

Iterative implementation:

Note:

Postorder Traversal is left->right->root, however, the reverse one is root>right>left, which is a modified version of preorder traversal. This can convert postorder traversal to some kind of preorder traversal.

Use Case

Example: Postorder Traversal for gathering file sizes

Level order Traversal

Level order traversal means we “Visit” nodes on 0th level, then 1st level, then 2nd level, etc.

Video of level-order traversal

Recursive implementation:

Note:

Sometimes, we need to know the max depth of a tree before further execution.

Iterative implementation:

Note:

Level order traversal use a queue instead of a stack

The code structure of level order traversal is like pre-order traversal one besides the push and pop action of the queue.

Practice & Practice & Practice

Here is a list of problem to test your tree traversal skills from another post

Have fun!