Leetcode - 1372. Longest ZigZag Path in a Binary Tree
Question / Problem: Leetcode
Solution
class Solution {
int find(TreeNode root, int isLeft, int count){
if(root == null) return count;
if(isLeft == 1)
return Math.max(find(root.right,0,count+1), find(root.left,1,0));
return Math.max(find(root.left,1,count+1), find(root.right,0,0));
}
public int longestZigZag(TreeNode root) {
return Math.max(find(root.left,1,0), find(root.right,0,0));
}
}
Algorithm
Current is left, and going to right then increment by 1 else reset to 0, Wiseversa
DP till to find the max between left and right possibility
return the Maximum integer
Readers
If you feel this is helpful, please do leave a Like / Comment. If you feel I can improve better, please leave comments where I can improve. Any suggestions are help