# Traverse binary tree in level order by spiral

Another puzzle is at stake, folks. This time it is binary tree related (not necessarily binary search tree as we are not interested in data relations but rather in binary tree structure). We need to traverse binary tree level by level (level is defined as set of all nodes at the same distance from root) in such a way that traversal direction within level changes from level to level thus forming a spiral. For example, consider binary tree below (it is rotated 90 degrees counter clockwise). Asterisk means NIL node.

```
*
20
*
19
*
18
*
17
*
13
*
12
*
11
*
10
*
7
*
6
*
5
*
4
*
2
*
```

Desired traversal for this tree will be: 10, 4, 11, 12, 6, 2, 5, 7, 19, 20, 17, 13, 18. Dissected into levels it looks like:

- 10
- 4, 11
- 12, 6, 2
- 5, 7, 19
- 20, 17
- 13, 18

It may not be obvious how to approach the problem. We’ll start with a well know problem of traversing binary tree level by level (also known as breadth first traversal). It can be done using queue.

```
static IEnumerable<T> LevelOrderLeftToRight<T>(TreeNode<T> root)
{
if (root == null)
yield break;
var next = new Queue<TreeNode<T>>();
next.Enqueue(root);
while(next.Count > 0)
{
var node = next.Dequeue();
yield return node.Data;
EnqueueIfNotNull(next, node.Left);
EnqueueIfNotNull(next, node.Right);
}
}
static void EnqueueIfNotNull<T>(Queue<T> queue, T value)
where T:class
{
if (value != null)
queue.Enqueue(value);
}
```

The queue contains yet to be examined nodes in level by level left to right (as we first enqueue left child and then right) order. At each step node at the front is dequeued, examined and its children are enqueued for further examination thus preserving level by level left to right order. It yields the following traversal: 10, 4, 11, 2, 6, 12, 5, 7, 19, 17, 20, 13, 18. Let’s split node sequence into levels:

- 10
- 4, 11
- 2, 6, 12
- 5, 7, 19
- 17, 20
- 13, 18

That looks close to sought-for order except every odd numbered level has must be reversed. With all nodes in the same container (queue) it is hard to do this. Let’s change code a little bit to make every two adjacent layers are separated into different containers.

```
static IEnumerable<T> LevelOrderLeftToRight<T>(TreeNode<T> root)
{
if (root == null)
yield break;
var curr = new Queue<TreeNode<T>>();
var next = new Queue<TreeNode<T>>();
next.Enqueue(root);
do
{
// Swap level containers
Swap(ref curr, ref next);
// Examine all nodes at current level
while (curr.Count > 0)
{
var node = curr.Dequeue();
yield return node.Data;
// Fill next level preserving order
EnqueueIfNotNull(next, node.Left);
EnqueueIfNotNull(next, node.Right);
}
// Continue until next level has nodes
} while (next.Count > 0);
}
static void Swap<T>(ref T a, ref T b)
{
var tmp = a;
a = b;
b = tmp;
}
```

With adjacent levels separated we can change order within levels. Next level is a set of child nodes from current level. FIFO container (queue) makes sure that child nodes of earlier examined node will also be examined earlier. But this is opposite to what we are looking for. Change it to LIFO container (stack)! And that’s it. Child nodes of earlier examined node will be examined later.

```
static IEnumerable<T> LevelOrderBySpiral<T>(TreeNode<T> root)
{
if (root == null)
yield break;
var curr = new Stack<TreeNode<T>>();
var next = new Stack<TreeNode<T>>();
// Specifies direction for the next level
var leftToRight = true;
next.Push(root);
do
{
Swap(ref curr, ref next);
while (curr.Count > 0)
{
var node = curr.Pop();
yield return node.Data;
// If next level must be traversed from left to right
// we must first push right child node and then left
// and in opposite order if next level will be
// traversed from right to left
PushIfNotNull(next, leftToRight ? node.Right : node.Left);
PushIfNotNull(next, leftToRight ? node.Left : node.Right);
}
// Change direction within level
leftToRight = !leftToRight;
} while (next.Count > 0);
}
static void PushIfNotNull<T>(Stack<T> stack, T value)
where T : class
{
if (value != null)
stack.Push(value);
}
```

The code yields sought-for order.