2
* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode3
* right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left,4
* TreeNode right) { this.val = val; this.left = left; this.right = right; } }13
public int longestZigZag(TreeNode root) {14
Pair ans = longestZigZag_(root);18
public Pair longestZigZag_(TreeNode root) {19
if (root == null) return new Pair();20
Pair l = longestZigZag_(root.left);21
Pair r = longestZigZag_(root.right);23
Pair myAns = new Pair();24
myAns.left = l.right + 1;25
myAns.right = r.left + 1;26
int max = Math.max(myAns.left, myAns.right);27
myAns.maxLen = Math.max(max, Math.max(l.maxLen, r.maxLen));