<p>You are given the <code>root</code> of a binary tree.</p>

<p>A ZigZag path for a binary tree is defined as follow:</p>

<ul>
	<li>Choose <strong>any </strong>node in the binary tree and a direction (right or left).</li>
	<li>If the current direction is right, move to the right child of the current node; otherwise, move to the left child.</li>
	<li>Change the direction from right to left or from left to right.</li>
	<li>Repeat the second and third steps until you can&#39;t move in the tree.</li>
</ul>

<p>Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).</p>

<p>Return <em>the longest <strong>ZigZag</strong> path contained in that tree</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/22/sample_1_1702.png" style="width: 221px; height: 383px;" />
<pre>
<strong>Input:</strong> root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Longest ZigZag path in blue nodes (right -&gt; left -&gt; right).
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/22/sample_2_1702.png" style="width: 157px; height: 329px;" />
<pre>
<strong>Input:</strong> root = [1,1,1,null,1,null,null,1,1,null,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Longest ZigZag path in blue nodes (left -&gt; right -&gt; left -&gt; right).
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 100</code></li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution { // divide and conquer (διαίρει καὶ βασίλευε, divide et vince)
    tuple<int, int, int> DFS(TreeNode* root) { // (¶)
        if (!root) return {-1, -1, 0};
        auto left = DFS(root->left), right = DFS(root->right);
        int leftLen = 1 + get<1>(left), rightLen = 1 + get<0>(right), maxLenLeftSubtree = get<2>(left), maxLenRightSubtree = get<2>(right), currentMax = max({leftLen, rightLen, maxLenLeftSubtree, maxLenRightSubtree}); // (§)
        return {leftLen, rightLen, currentMax};
    }
public: // ⚡電光形（ジグザグ、でんこうけい）ϟ♒
    int longestZigZag(TreeNode* root) {
        return get<2>(DFS(root)); // ie returns root.currentMax
    }
};
// (¶) returns max zigzag path length starting at `root` with first direction being left, and right, as well as the global max (ie global longest length) in that subtree, that is, returns {leftLen, rightLen, currentMax}
/* (§) Consider this subtree. We now invoke the function DFS(a)
         a
       /   \
      b     c
     / \   / \
    d   e f   g
   / \ /\ /\ / \ 
*/
// `leftLen` is the length of the zigzag path starting at node a and then turning left first, that is, the zigzag path starting with node a then node b then node e, that is why `leftLen` is assigned to 1+DFS(b)[1] (for a more readable notation, we can write a.leftLen = 1 + b.rightLen). `rightLen` can be obtained similarly (a.rightLen = 1 + c.leftLen). `maxLenLeftSubtree` (ie b.currentMax) is the length of the longest zigzag path in subtree rooted at b. For a more readable notation, we can write a.currentMax = max(a.leftLen, a.rightLen, b.currentMax, c.currentMax)
101840