Current streak:
0 days
Longest streak:
4 days
Less
More
code here:
public static int diameterOfBinaryTree(TreeNode<Integer> root) {
// Write your code here.
int[] maximum = new int[1];
maximum[0] = -1;
getDiameter(root, maximum);
return maximum[0];
}
public static int getDiameter(TreeNode<Integer> root, int[] maximum) {
if(root == null)
return 0;
int lh = getDiameter(root.left, maximum);
int rh = getDiameter(root.right, maximum);
//max[0] = Math.max(max[0], 1 + lh + rh);
maximum[0] = Math.max(maximum[0], (lh+rh)) ;
return 1 + Math.max(lh, rh);
}