Tree traversal refers to the process of visiting each node in a tree data structure (Wikipedia). The two general strategies are Depth-First-Search (DFS) and Breadth-First-Search (BFS). For BFS, it iterates through the tree level by level with Queue, from top to bottom. When using DFS, there are three different ways: Preorder, Inorder, and Postorder.

If we use V, L, and R to represent visiting the current node, moving to the left subtree, and moving to the right subtree, there are 6 possible traversals: **V**LR, **V**RL, L**V**R, R**V**L, LR**V**, RL**V**. If the left node is constrained to be visited before the right node, only three traversals remain: **V**LR, L**V**R, and LR**V**, with the names of Preorder traversal, Inorder traversal, and Postorder traversal accordingly.

We can implement each of them using recursions with the system’s execution stack and using iterations with a self-defined stack. Notably, the key differences between recursions and iterations are:

- A conditional statement determines the exit of a recursion, while a control variable’s value determines the termination of an iteration.
- Infinite recursions lead to stack overflow, while infinite iterations consume CPU cycles.
- Sometimes recursions are comparatively slower than iterations.

## Preorder – V L R

To traverse a tree in preorder, we need to:

- Visit the current node
- Move to the left subtree and start traversing in preorder
- Move to the right subtree and start traversing in preorder

### Recursion

```
def preorder(node):
if node is None:
return
visit(node)
preorder(node.left)
preorder(node.right)
```

### Iteration

```
stack = [root]
while stack:
node = stack.pop()
visit(node)
# push the right node to the stack before the left one
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
```

Or, we don’t really have to push every node into the stack – for each subtree, we push the right child into the stack and access the left child – to save space.

```
stack = [root]
while stack:
node = stack.pop()
while node:
visit(node)
if node.right:
stack.append(node.right)
node = node.left
```

## Inorder – L V R

To traverse a tree in inorder, we need to:

- Move to the left subtree and start traversing in inorder
- Visit the current node
- Move to the right subtree and start traversing in inorder

### Recursion

```
def inorder(node):
if node is None:
return
inorder(node.left)
visit(node)
inorder(node.right)
```

### Iteration

```
stack = []
cur = root
while stack or cur:
if cur:
stack.append(cur)
cur = cur.left
else:
cur = stack.pop()
visit(cur)
cur = cur.right
```

## Postorder – L R V

To traverse a tree in postorder, we need to:

- Move to the left subtree and start traversing in postorder
- Move to the right subtree and start traversing in postorder
- Visit the current node

### Recursion

```
def postorder(node):
if node is None:
return
postorder(node.left)
postorder(node.right)
visit(node)
```

### Iteration

When we implement the traversal in preorder, we access the node first, and then push the right child and left child into the stack. So for postorder, are we merely switch the orders of these operations – push the right and left child into the stack and access the node?

The answer is **NO** – after switching, we are still traversing in preorder. Because what defines preorder is not accessing the node before pushing its children into the stack, but is poping the node out of the stack before pushing the children into it.

However, it does not work for postorder. To implement the iterative way of traversing in postorder, we notice that LRV (postorder) is the reversed VRL (modified preorder).

We visit nodes in VRL with the first stack – by switching L and R in VLR (preorder) – and push the nodes into the second stack. After pushing the tree into the stack, we pop it one by one. Now we have the postorder!

```
stack = [root]
while stack:
node = stack.pop()
reverse_stack.append(node)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
while reverse_stack:
visit(reverse_stack.pop())
```

## Practices

Leetcode problems are available here: [Leetcode for Interview] BinaryTree Traversal – DFS & BFS

##### References

- Drozdek, A. (2012). Data Structures and algorithms in C++. Cengage Learning.
- https://cloud.tencent.com/developer/ask/129134
- https://medium.com/backticks-tildes/iteration-vs-recursion-c2017a483890