Write an efficient algorithm to invert alternate levels of a perfect binary tree.

For example, consider the following tree:

Perfect Binary Tree

We should convert it into the following tree:

Inverted Perfect Binary Tree

Practice this problem

1. Using Level Order Traversal

The idea is to perform a level order traversal of the perfect binary tree and traverse its nodes level-by-level. Then for each odd level, push all nodes present in that level into a stack. Finally, at the end of each odd level, we put nodes present in the stack into their correct position. Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Java


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Python


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

The time complexity of the above solution is O(n), where n is the total number of nodes in the binary tree. The program requires O(n) extra space for storing nodes present at odd levels of a binary tree. The stack is preferred over a list for storing nodes since it is a LIFO data structure, and we don’t need to reverse it before assigning value to nodes.

2. Using Inorder Traversal

The idea remains similar to the previous approach, except here we recursively traverse the tree in an inorder fashion, and store nodes present all odd levels in a stack, and replace them later by doing another inorder traversal. This approach is demonstrated below in C++, Java, and Python:

C++


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Java


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Python


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

The time complexity of the above solution is O(n), where n is the total number of nodes in the binary tree. The program requires O(n) extra space for storing nodes of odd levels.

 
We can replace stack with queue by doing reverse inorder traversal in the pushOddLevelNodes() function, i.e., call the right child before the left child. Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Java


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Python


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

3. Using Preorder Traversal

The above solution requires two traversals of the binary tree and also needs a stack data structure. We can invert alternate levels of a perfect binary tree in single tree traversal and without any explicit stack. The idea is to perform a modified preorder traversal of the tree and use level information to swap the data within the function itself. It is a little tricky and requires special attention.

The implementation can be seen below in C++, Java, and Python:

C++


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Java


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Python


Download  Run Code

Output:

1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

The time complexity of the above solution is O(n), where n is the total number of nodes in the binary tree. The program requires O(h) extra space for the call stack, where h is the height of the tree.