package leetcode;

/**
 * https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/
 */
public class Problem1372 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int longestZigZag(TreeNode root) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
    }
}

@@ -340,15 +340,186 @@
     
-// TODO
+return Math.max(longestZigZag(root, true), longestZigZag(root, false)) - 1;%0A    %7D%0A%0A    private static int longestZigZag(TreeNode root, boolean left) %7B%0A        if (root == null) %7B
 %0A   
@@ -519,24 +519,28 @@
 ) %7B%0A        
+    
 return 0;%0A  
@@ -545,55 +545,691 @@
     
-%7D%0A%0A    public static void main(String%5B%5D args) %7B
+    %7D%0A        if (left) %7B%0A            return longestZigZag(root.left, !left) + 1;%0A        %7D%0A        return longestZigZag(root.right, !left) + 1;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1372 prob = new Problem1372();%0A%0A        TreeNode root = new TreeNode(1);%0A        root.right = new TreeNode(1);%0A        root.right.left = new TreeNode(1);%0A        root.right.right = new TreeNode(1);%0A        root.right.right.left = new TreeNode(1);%0A        root.right.right.right = new TreeNode(1);%0A        root.right.right.left.right = new TreeNode(1);%0A        root.right.right.left.right.right = new TreeNode(1);%0A        System.out.println(prob.longestZigZag(root)); // 3
 %0A   

Update problem 1372