#103 Binary Tree Zigzag Level Order Traversal
frequently reported atAmazonMicrosoftBloombergLinkedInGoogleMeta
the problem
In plain words
Return the values level by level, alternating direction: level 0 left to right, level 1 right to left, level 2 left to right again, and so on.
in root = [3,9,20,null,null,15,7]
out [[3],[20,9],[15,7]]
in root = [1]
out [[1]]
in root = []
out []
- 0 ≤ number of nodes ≤ 2000
- -100 ≤ Node.val ≤ 100
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternTree BFS (level order)
Keep the BFS exactly as it is — nodes always come out of the queue left to right. Change only where each value goes in its level's list: at the back on even levels, at the front on odd levels.
solutions
From brute force to optimal
Normal size-snapshot BFS; before storing an odd level, Collections.reverse it.
- BFS one level at a time.
- If the level number is odd, reverse its list.
- Add it to the answer.
Each node is polled once; reversing a level costs its size, so all reversals add up to n.
The queue and the current level.
class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> levels = new ArrayList<>(); if (root == null) return levels; Deque<TreeNode> queue = new ArrayDeque<>(); queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); List<Integer> level = new ArrayList<>(size); for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); level.add(node.val); if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); } if (levels.size() % 2 == 1) Collections.reverse(level); // odd levels read right to left levels.add(level); } return levels; }}Collect each level in a Deque: addLast when going left to right, addFirst when going right to left.
- leftToRight = true.
- Per level: poll size nodes; addLast or addFirst each value.
- Store the level as a list; flip leftToRight.
Each node polled once; addFirst/addLast are O(1).
The queue and the current level.
class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> levels = new ArrayList<>(); if (root == null) return levels; Deque<TreeNode> queue = new ArrayDeque<>(); queue.offer(root); boolean leftToRight = true; while (!queue.isEmpty()) { int size = queue.size(); Deque<Integer> level = new ArrayDeque<>(size); // O(1) adds at either end for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); // nodes always come out left to right if (leftToRight) level.addLast(node.val); else level.addFirst(node.val); // right-to-left level: prepend instead if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); } levels.add(new ArrayList<>(level)); leftToRight = !leftToRight; // flip direction for the next level } return levels; }}in the room
How to say it in the interview
talk track
- Write plain level order first, then add the one-line twist.
- Say why you don't change the order children are offered: it would scramble the next level.
traps
- Swapping the order you offer left and right children on odd levels — the next level comes out in a mixed, wrong order.
- Prepending with
ArrayList.add(0, x): each call shifts the whole list, O(w²) per level. Use a Deque, or reverse once.
what they ask next
Follow-ups
Two-stack version?
Pop from the current stack and push children onto the next one — left then right on even levels, right then left on odd levels. Also O(n).
Bottom-up level order (LC 107)?
Normal BFS, then Collections.reverse(levels) — or addFirst each level into a LinkedList.
before you submit
Edge cases to test
- Empty tree → [].
- One node → [[1]].
- A chain: one value per level, so the direction never shows — but the code must still flip correctly.
practise next
Similar problems
- #102Binary Tree Level Order TraversalMediumin Traversals
- #637Average of Levels in Binary TreeEasyin Traversals