```
public class Solution {
public int diameterOfBinaryTree(TreeNode root) {
helper(root);
return diameter;
}
int diameter = 0;
private int helper(TreeNode node) {
if (node == null) return 0;
int left = helper(node.left);
int right = helper(node.right);
diameter = Math.max(diameter, left + right);
return Math.max(left, right) + 1;
}
}
```