# Binary Tree Traversals

The goal of this document is to present a collection of things about binary tree traversals to get the reader up to speed while solving simple tree related problems. (Be it interview prep or competitive programming or application programming; This document will try to act as a quick intro/revision)

Trees are actually a kind of graph. Hence it could be traversed/searched in two ways (most of the times, this viewpoint is not presented in textbooks because we are used to learning graphs after trees.)

1. Depth-first

Traversal = Visiting each node of the tree exactly once
Search = Given a value, search if a node in the tree contains that value

Both Traversal and search on a tree could be done via both depth-first and breadth-first manner.

## Structure

Before solving the traversal problems, I would like to present the structure of the binary tree.

This is the structure used by the leetcode, but it will be similar elsewhere.

## Depth-first

Here is a simple image from Wikipedia for a warm up. Various types of depth-first traversal/search are

1. Pre-Order (NLR)
2. In-Order (LNR)
3. Post-Order (LRN)

Traversals are usually classified based on the order in which the root node is visited.

If the root node is visited before other nodes, then it is called PRE-order.
If the root node is visited after other nodes, then it is called POST-order.
If the root node is visited in-between the visits of other nodes, then it is called IN-order.

Usually, the order in which the children nodes are visited does not matter i.e. left before right and right before left. But most materials and implementations do left before right.

Remember that the key to solving Depth-first traversal/search would be to use a stack - call stack in case of recursion and a stack implementation in case of an iterative approach. Don’t worry much about this, but this going to be your take-home of the day and if you don’t get this pattern in the first go, don’t be worried; you will eventually get it!

Before moving on, I would like to state a very useful resource - Wikipedia page, that contains crisp and clear pseudocode for most of the code written here. So do check it out!

### Pre-order

The root node is visited before visiting the children.

Pre-order for the example tree given in the above figure would be FBADCEGIH

Recursive Solution

Iterative Solution

### In-order

Nodes are visited in left, root and right order.

I just noticed the term “Out-order” in the Wikipedia page, for which the nodes are visited in right, root and left order. Some textbooks do not mention this explicitly.

This traversal seems to be useful especially for binary search trees. Example, in-order traversal of a BST gives ascending order of values present in it, whereas the out-order gives the descending order.

In-order for the example tree given in the above figure would be ABCDEFGHI

Out-order for the example tree given in the above figure would be IHGFEDCBA

Recursive Solution

Iterative Solution

The iterative of in-order could seem complex at first. So, I suggest you to go through videos that explain it by tracing out on a whiteboard and eventually tracing out by yourself on a piece of paper.

### Post-order

The root node is visited after visiting the children.

Post-order for the example tree given in the above figure would be ACEDBHIGF

Recursive Solution

Iterative Solution

This could be done in a couple of ways. One is to use two stacks and other is to build upon the algorithm used for in-order.

To be simple, I will list out the two stacks method here.

Breadth-first traversal/search, on the other hand, looks like I love doing this so much that I often try to solve any tree problem thrown at me with this first. haha. Why? This is probably one of the algorithms where a queue is used elegantly to solve the problem and you could use this technique to solve a lot of tree problems.

This is also referred to as level-order traversal sometimes.

I would like to show a few variations of the solution. Because these variations might often help you in implementing level-order and tweaking it a bit to arrive at your custom solution.

Using one queue and get a result back as a simple list of visited values

Using two queues for level-order traversal. (Not so elegant. hmmm, yeah!)

The following code does the same thing as above but avoids a `while(true)`.

## Complexity

All the above solutions have

Time Complexity = O(N)
Space Complexity = O(N)

Because you visit every node and allocate call-stack/stack/queue for N nodes.

If you want a more space efficient solution, check out Morris Traversal.

## What to choose?

With all these solutions for solving a problem put in front of you, you might be wondering which way to choose. So here is a small piece on it.

Most of the times, go with iterative stack/queue implementation.

Recursive is clean and help you to think about a lot of tree problems. But for larger trees, you might face call-stack overflow. On the other hand, the iterative stack/queue is allocated on the heap and often times heap is big enough to accommodate your nodes.

At the same time, while interviewing don’t try to solve by Morris traversal unless explicitly mentioned by the interviewer. Because it is time-consuming and often not clear. Even while writing real-world code, you might go with iterative implementation and then refactor to Morris (when you have time for it).

## Closing Notes

1. If Depth-first, think in stacks
2. If Breadth-first, think in queues
3. All solutions above have time complexity = O(N) because each node is visited once.
4. All solutions above have space complexity = O(N) because of call-stack/stack/queue space used.
5. For a more efficient (but complex) solution, check out Morris traversal.
1. Tree Traversals - https://en.wikipedia.org/wiki/Tree_traversal
2. Post-order two stacks - https://www.youtube.com/watch?v=qT65HltK2uE