```
public int DiameterOfBinaryTree(TreeNode root) {
int d = 0;
Recurse(root, ref d);
return d;
}
public int Recurse(TreeNode node, ref int max) {
if (node == null) return 0;
int left = Recurse(node.left, ref max);
int right = Recurse(node.right, ref max);
max = Math.Max(max, left + right);
return Math.Max(left, right) + 1;
}
```