Skip to content

Practice Longest ZigZag Path in a Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1372
LeetCode Problem

Longest ZigZag Path in a Binary Tree

Problem Statement

A path may start at any node and initially choose its left or right child. Continue downward, alternating left and right on successive edges. Return the greatest number of edges in such a path; a single node has length 0.

Source: Longest ZigZag Path in a Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

Input
root = [1,1,1,null,1,null,null,1,1,null,1]
Output
4

# Example 3

Input
root = [1]
Output
0

Constraints

The number of nodes in the tree is in the range [1, 5 * 10^4].
1 <= Node.val <= 100

Related LeetCode Problems

Practice more problems similar to Longest ZigZag Path in a Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.