2Brute2Best
🔥0

Longest ZigZag Path in a Binary Tree

Medium·LeetCode #1372 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

You are given the root of a binary tree.

A zigzag path starts at any node. Its moves alternate: a move to the left child, then a move to the right child, then left again, and so on. The first move can be either way. The length of the path is the number of moves. A single node has length 0. Return the length of the longest zigzag path in the tree.

Input

The root of a binary tree. It has at least one node.

Output

The number of moves in the longest zigzag path.

Example 1
Input: root = [1, null, 1, 1, 1, null, null, 1, 1, null, 1, null, null, null, 1]
Output: 3

The longest zigzag goes right, then left, then right. That is 3 moves.

Example 2
Input: root = [1, 1, 1, null, 1, null, null, 1, 1, null, 1]
Output: 4

The longest zigzag goes left, right, left, right. That is 4 moves.

Example 3
Input: root = [1]
Output: 0

A single node has no moves.

Constraints
  • • The tree has between 1 and 5 × 10^4 nodes.
  • • 1 ≤ Node.val ≤ 100
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“moves alternate: left, right, left”
→ A zigzag changes direction at every step. A straight line (left, left) is not a zigzag.
“the path can start at any node”
→ The answer is the best over all nodes, so every node has to be checked.
“length is the number of moves”
→ Count the edges (moves), not the nodes. A single node has length 0.
03

Approach Complexity Ladder

Brute Force — walk a zigzag from every nodeO(N · H)O(H)

From each node, follow the zigzag in both first directions and keep the longest walk

Optimal — carry both zigzag lengths downO(N)O(H)

Pass down (length if the last move was left, length if it was right); both update at each child

Brute Force Intuition

For every node and each first direction, walk down while the moves keep alternating, and stop when the child is missing. Count the moves. Do this for every node, and keep the biggest count.

Recognition rule for this sub-pattern:

Carry information down the path (current sum, max so far, direction). Check it at leaves.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.