Longest ZigZag Path in a Binary Tree

medium tree dfs

Problem

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. After each move, change the direction (left becomes right and vice versa). Repeat until you can no longer move (there is no matching child). The ZigZag length is the number of nodes visited minus 1 (that is, the number of edges). Return the longest ZigZag path length in the tree, taken over all starting nodes and directions. In the input notation, _ denotes a null node.

Inputtree = [1, 1, _, _, 1, 1]
Output3
A left-right-left chain of 3 edges (_ denotes a null node).

def longest_zigzag(root):
    best = 0
    def dfs(n):
        nonlocal best
        if not n: return (-1, -1)
        ll, lr = dfs(n.left)
        rl, rr = dfs(n.right)
        left, right = lr + 1, rl + 1
        if left > best: best = left
        if right > best: best = right
        return (left, right)
    dfs(root)
    return best
function longestZigzag(root) {
  let best = 0;
  function dfs(n) {
    if (!n) return [-1, -1];
    const [ll, lr] = dfs(n.left);
    const [rl, rr] = dfs(n.right);
    const left = lr + 1, right = rl + 1;
    if (left > best) best = left;
    if (right > best) best = right;
    return [left, right];
  }
  dfs(root);
  return best;
}
class Solution {
    int best;
    public int longestZigzag(TreeNode root) {
        best = 0;
        dfs(root);
        return best;
    }
    private int[] dfs(TreeNode n) {
        if (n == null) return new int[]{ -1, -1 };
        int[] L = dfs(n.left);
        int[] R = dfs(n.right);
        int left = L[1] + 1, right = R[0] + 1;
        if (left > best) best = left;
        if (right > best) best = right;
        return new int[]{ left, right };
    }
}
int best_;
pair<int,int> dfs(TreeNode* n) {
    if (!n) return {-1, -1};
    auto [ll, lr] = dfs(n->left);
    auto [rl, rr] = dfs(n->right);
    int left = lr + 1, right = rl + 1;
    if (left > best_) best_ = left;
    if (right > best_) best_ = right;
    return {left, right};
}
int longestZigzag(TreeNode* root) {
    best_ = 0;
    dfs(root);
    return best_;
}
Time: O(n) Space: O(h)