java.lang.Object
g1301_1400.s1372_longest_zigzag_path_in_a_binary_tree.Solution

public class Solution extends Object
1372 - Longest ZigZag Path in a Binary Tree.<p>Medium</p> <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&rsquo;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><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/01/22/sample_1_1702.png" alt="" /></p> <p><strong>Input:</strong> root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Longest ZigZag path in blue nodes (right -> left -> right).</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/01/22/sample_2_1702.png" alt="" /></p> <p><strong>Input:</strong> root = [1,1,1,null,1,null,null,1,1,null,1]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> Longest ZigZag path in blue nodes (left -> right -> left -> right).</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> root = [1]</p> <p><strong>Output:</strong> 0</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 <= Node.val <= 100</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestZigZag

      public int longestZigZag(TreeNode root)