classSolution{int ret =0;publicintdiameterOfBinaryTree(TreeNode root){findDepth(root);return ret -1;// Need edges count only}// DFSprivateintfindDepth(TreeNode root){if(root ==null)return0;// node countsint leftPath =findDepth(root.left);int rightPath =findDepth(root.right);// Current node as turning node
ret =Math.max(ret, leftPath +1+ rightPath);// return node count, so add 1 as current nodereturnMath.max(leftPath, rightPath)+1;}}