Advertisement

Longest ZigZag Path in a Binary Tree - LeetCode 1372 Solution

Longest ZigZag Path in a Binary Tree - Complete Solution Guide

Longest ZigZag Path in a Binary Tree is LeetCode problem 1372, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given the root of a binary tree. A ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right, move to the right child of the current node; otherwise, move to the left child. Change the direction from right to left or from left to right. Repeat the second and third steps until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of

Detailed Explanation

The problem asks us to find the longest ZigZag path in a given binary tree. A ZigZag path is defined as a path starting from any node and direction (left or right), and alternating directions (left to right or right to left) until we cannot move further. The length of the path is the number of nodes visited minus 1. The task is to find the maximum length of such a ZigZag path within the given tree.

Solution Approach

The provided solution uses a recursive Depth-First Search (DFS) approach with dynamic programming. The `dfs` function (or `solve` function in Java/C/C++) computes the length of the longest ZigZag paths starting at a given node, going both left and right. It stores the results (the left and right path lengths) and also keeps track of the overall maximum length found so far using a global variable. The post-order traversal ensures that the children are processed before their parents, allowing us to build the solution from the bottom up.

Step-by-Step Algorithm

  1. Step 1: Define a recursive function (dfs/solve) that takes a node as input and returns a pair of integers: (left_path_length, right_path_length). These represent the longest zigzag path lengths starting at the given node going left and right respectively.
  2. Step 2: Base Case: If the node is null, return (-1, -1). This ensures that when we add 1 to the length, it becomes 0 for the starting node (as the initial edge doesn't exist).
  3. Step 3: Recursively call the function for the left and right children of the current node.
  4. Step 4: Calculate the length of the ZigZag path starting at the current node and going left. This is equal to 1 + the length of the longest ZigZag path starting at the left child and going right (left_path = 1 + right_path_from_left_child).
  5. Step 5: Calculate the length of the ZigZag path starting at the current node and going right. This is equal to 1 + the length of the longest ZigZag path starting at the right child and going left (right_path = 1 + left_path_from_right_child).
  6. Step 6: Update the global variable `max_length` with the maximum of its current value, `left_path`, and `right_path`.
  7. Step 7: Return the pair (left_path, right_path) for the current node.

Key Insights

  • Insight 1: Dynamic Programming: The problem can be solved efficiently using dynamic programming by calculating the longest ZigZag path starting at each node in both left and right directions. This avoids redundant calculations.
  • Insight 2: Post-order Traversal: A post-order traversal of the tree is crucial because to determine the length of the ZigZag path originating from a node, we need the lengths of ZigZag paths starting at its children.
  • Insight 3: Recursive Approach: The problem lends itself naturally to a recursive implementation, where each recursive call computes the ZigZag path lengths starting from a given node.
  • Insight 4: State at each Node: For each node, we need to maintain two states: the longest zigzag path starting from the node going left and the longest zigzag path starting from the node going right. This information allows us to build the solution recursively.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Dynamic Programming, Tree, Depth-First Search, Binary Tree.

Companies

Asked at: eBay.