Tap here to switch tabs
Problem
Submissions

Problem: Longest ZigZag Path in a Binary Tree

Try to solve the Longest ZigZag Path in a Binary Tree problem.

Statement

Given the root of a binary tree, return the maximum length of a ZigZag path contained in the tree.

A ZigZag path is defined as follows:

  • Start at any node in the tree and select an initial direction (left or right).

  • If the current direction is right, move to the right child of the current node; otherwise, move to the left child.

  • Alternate the direction after each step (switch from right to left, or left to right).

  • Repeat this movement until you can no longer proceed.

The length of a ZigZag path is defined as the number of edges traversed (a path with a single node has a length of 00).

Constraints:

  • The number of nodes in the tree is in the range [1,5imes104][1, 5 imes 10^4].

  • 1≤1 \leq Node.val ≤100\leq 100

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Longest ZigZag Path in a Binary Tree

Try to solve the Longest ZigZag Path in a Binary Tree problem.

Statement

Given the root of a binary tree, return the maximum length of a ZigZag path contained in the tree.

A ZigZag path is defined as follows:

  • Start at any node in the tree and select an initial direction (left or right).

  • If the current direction is right, move to the right child of the current node; otherwise, move to the left child.

  • Alternate the direction after each step (switch from right to left, or left to right).

  • Repeat this movement until you can no longer proceed.

The length of a ZigZag path is defined as the number of edges traversed (a path with a single node has a length of 00).

Constraints:

  • The number of nodes in the tree is in the range [1,5imes104][1, 5 imes 10^4].

  • 1≤1 \leq Node.val ≤100\leq 100