Reported September 2026
LinkedInbreadth first search

Binary Tree Zigzag Level Order Traversal

Reported by candidates from LinkedIn's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live LinkedIn OA. Under 2s to a working solution.
Founder's read

The LinkedIn OA reported in September 2026 gives you a binary tree and asks for a zigzag level order traversal. It looks friendly, but the input is a tree of unknown size, so anything that rescans the tree per level or rebuilds lists over and over will hurt. The pattern is breadth-first search with a direction flag. Left to right on level one, right to left on level two, and so on. If your head goes blank under the timer, StealthCoder runs invisibly on your desktop and can hand you a working solution live. Know the trick first, though, because it's about ten lines.

The problem

Given the root of a binary tree, return its values level by level in zigzag order.
Read the root level from left to right, the next level from right to left, and continue alternating directions for every later level.

Function
zigzagLevelOrder(root: TreeNode) → int[][]

Examples
Example 1
root = [3,9,20,null,null,15,7]
return = [[3],[20,9],[15,7]]
The second level reverses direction, while the third level returns to left-to-right order.
Example 2
root = [1,2,3,4,null,null,5]
return = [[1],[3,2],[4,5]]
Missing children do not create output entries, and direction still alternates by level.

Constraints
The tree is finite and acyclic.
Every node value is a signed integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Run a standard BFS with a queue. Process one level at a time by snapshotting the queue size, collect the node values for that level, then flip a boolean after each level. Reverse the level list when the flag says right to left, or write into a pre-sized array from the far end. Either way you touch each node once, so it's O(n) time. The common pitfall is reversing the traversal order of children instead of the output order. That scrambles the next level's parents. Keep enqueueing left then right every time, and only change how you record values. Also handle the empty root and skewed trees, where every level has one node. Missing children never produce entries, so don't push nulls. If the live OA freezes you on the level-size snapshot, StealthCoder is the hedge that shows the exact loop.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Binary Tree Zigzag Level Order Traversal cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as binary tree zigzag level order traversal. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass LinkedIn's OA.

LinkedIn reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Binary Tree Zigzag Level Order Traversal FAQ

What's the trick in the LinkedIn zigzag traversal problem?+

It's plain level order BFS plus a direction toggle. Always enqueue left then right. For each level, collect values, then reverse that level's list when the toggle says right to left. Flip the toggle after every level. Don't change the queue order.

How hard is this really?+

It's a medium on paper but easy if you already know level order traversal. The only new idea is alternating direction. If you can write BFS with a per-level loop, you can finish this in a few minutes and spend the rest on edge cases.

Can I solve it with DFS instead?+

Yes. Pass the depth into a recursive call, append the node value to the list for that depth, and insert at the front when the depth is odd. It's still O(n) if you use a deque or reverse afterward. BFS is simpler and less error-prone.

What edge cases should I test?+

An empty tree should return an empty list. Test a single node, a fully skewed tree, and Example 2 where children are missing. Missing children must not add entries, and the direction should still alternate by level, not by node count.

How do I prepare for this in 48 hours?+

Write level order traversal from memory twice, then add the direction flag. Practice the per-level size snapshot, since that's where most bugs live. Then run both examples by hand. That covers the whole pattern and the tree questions that tend to follow it.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with LinkedIn.

OA at LinkedIn?
Invisible during screen share
Get it