In 30 Day Leet Coding Challeneg, there is question for finding Diameter of Binary Tree. There is a test case which is passing when I run custom test but it is failing when I submit, not sure why. Is there a bug in my solution or in leetcode?
Here is my code:
class Solution {
static int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
Solution.getHeight(root);
return Solution.max;
}
public static int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
int leftHgt = Solution.getHeight(root.left);
int rightHgt = Solution.getHeight(root.right);
if (leftHgt+rightHgt > max) {
max = leftHgt+rightHgt;
}
return leftHgt > rightHgt ? leftHgt+1 : rightHgt+1;
}
}Test case that is failing on submission:
Input: []
Ouput should be: 0
It is 0 when I run **custom test** but when I **submit** it shows output