AfterAcademy Tech
•
04 May 2020

Difficulty: Medium
Asked in: Amazon, Microsoft
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (i.e, from left to right, then right to left for the next level and alternate between).
Example
Input: Given binary tree [5,8,2,null,null,6,4]
3
/ \
9 20
/ \
15 7
Output: Return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
The node structure passed to your function will be
class TreeNode
{
int val
TreeNode left
TreeNode right
}
You may try to solve this problem here.
If you will think about the problem, you will realize that this problem is basically a preorder or postorder traversal with the only difference that for each level, the traversal toggles between left to right and right to left.
However, the problems require to return a two-dimensional array that will contain the zig-zag traversal of the tree in a level order fashion.
So, We could create a res array and make a DFS or preorder tree traversal, while passing another argument called level. If the level is even, then we will add the root value in the res array in the end of the res[level] otherwise, at the beginning of res[level]. To reduce the time complexity of shifting, we can use an array of a doubly linked list and before returning, convert all the doubly linked lists into an array.
Example:

Solution step
index = levelindex = levelint[][] zigzagLevelOrder(TreeNode root) {
int[][] res
zigzagLevelOrder(root, res, 0)
return res
}
void zigzagLevelOrder(TreeNode root, int[][] res, int level) {
if (root is not null){
if (res.size() <= level)
res.append([])
if (level%2==0)
res[level].append(root.val) // Inserts at the end of the list
else
res[level].insert(0, root.val) // Inserts at the beginning of the list
zigzagLevelOrder(root.left, res, level+1)
zigzagLevelOrder(root.right, res, level+1)
}
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n), the space complexity is the height of the tree, which in worst-case is n.
Critical Ideas to Think
Solution steps
level list on each level to a new array. On each level, pop the last visited element from the Queue.flag could be useful that will help in toggle between the left to right and right to left traversal in the tree. A generalization could be for left to right traversal, flag is false, in which case simply add the polled node’s value to the level list, used for tracking the list of values on each particular level and vice versa.level list, explore it’s children if they exist.flag to achieve zig zag level order traversing. Continue to iterate the aforementioned steps until the queue is completely empty.Pseudo Code
int[][] zigzagLevelOrder(TreeNode root) {
int[][] res
if(root is null)
return res
//declare queue
Queue(TreeNode) queue
queue.add(root)
boolean zigzag = false
while( queue is not Empty) {
//declare level list and size from q
int[] level
int size = queue.size()
for (int i = 0 to i < size) {
//poll from q
TreeNode node = queue.pop()
if (zigzag) {
level.add(0, node.val)
} else {
level.add(node.val)
}
if (node.left is not null) {
queue.add(node.left)
}
if (node.right is not null) {
queue.add(node.right)
}
}
res.add(level)
zigzag = !zigzag
}
return res
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Critical Ideas to Think
zigzag = !zigzag does?level array?
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given a binary tree, write a program to return the average value of the nodes on each level in the form of an array. The range of the node's value is in the range of 32-bit signed integer.

AfterAcademy Tech
It is important to know traversal techniques in a tree before solving tree problems. We have discussed the 3 primary traversals in a tree at length in this blog with both recursive and iterative implementations.

AfterAcademy Tech
Given a binary tree and a sum, write a program to determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Its an easy problem based on Binary tree traversal and a famous interview question

AfterAcademy Tech
Given a binary tree, invert the binary tree and return it. An inverted form of a Binary Tree is another Binary Tree with left and right children of all non-leaf nodes interchanged. You may also call it the mirror of the input tree.
