Longest ZigZag Path in a Binary Tree
A medium-tier problem at 67% community acceptance, tagged with Dynamic Programming, Tree, Depth-First Search. Reported in interviews at eBay and 0 others.
Longest ZigZag Path in a Binary Tree is a medium-difficulty tree problem that shows up in real OAs, most recently at eBay. You're asked to find the longest alternating path in a binary tree where you switch between left and right children at each step. The catch: it's not immediately obvious that you need to track direction state in your recursion, and the greedy approach fails. If this problem hits your live assessment and the pattern doesn't click, StealthCoder runs invisibly during screen share and surfaces a working solution in seconds, so you keep moving.
Companies that ask "Longest ZigZag Path in a Binary Tree"
Longest ZigZag Path in a Binary Tree is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.
Get StealthCoderThe core trick is using DFS with state: you can't just track the longest path globally because you need to know which direction you came from to validate the next move. Set up a recursive function that returns a pair of values, one for paths ending with a left turn and one for paths ending with a right turn. At each node, the longest left-turn path is one plus the right-turn path of the left child, and vice versa. Common failure mode: trying to solve this with a single return value per node, which loses the direction context you need to extend paths correctly. The alternation constraint means you can't simply take max of both children at once. This is where Dynamic Programming on trees reveals its power, and where most candidates either don't see the state design or implement it messily.
Pattern tags
You know the problem.
Make sure you actually pass it.
Longest ZigZag Path in a Binary Tree recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest ZigZag Path in a Binary Tree interview FAQ
How hard is this problem really for a medium?+
With 66.6% acceptance, it's solidly medium. It's not a tree-traversal warmup, but it's not a hard-level constraint puzzle either. The difficulty is the state design, not the implementation. If you recognize the direction-tracking pattern, it's fast.
Is eBay still asking this, or should I skip it?+
eBay is the reported company. Interview pools shift, but eBay's OA often includes tree DP problems. It's worth covering, especially if you're in their loop. Even if it doesn't appear, the pattern (state-based DFS) applies to similar path problems.
What's the trick that makes this a DP problem?+
You must return two values from recursion: longest zigzag ending in a left move and longest ending in a right move. This dual-state return lets you enforce the alternation constraint when building paths up the tree. Single-return recursion can't encode the direction you arrived from.
Can I solve this with just DFS and no DP framing?+
Technically, yes, but it's messier. The DP lens makes it cleaner: you're storing optimal subproblems (best zigzag paths ending in each direction at each node). Without naming it DP, you'll likely recompute states or miss the pattern under pressure in a live OA.
How does this relate to other tree problems I've drilled?+
It combines Tree and Depth-First Search with Dynamic Programming principles. Unlike simple path-sum problems, you can't just sum up children. You have to track state through recursion, similar to problems requiring node-by-node memoization or multi-value returns.
Want the actual problem statement? View "Longest ZigZag Path in a Binary Tree" on LeetCode →