```
int diameterOfBinaryTree(TreeNode* root) {
int diam = 0;
height(root, diam);
return diam;
}
int height(TreeNode *node, int &diam) {
if (!node) return -1;
int left = height(node->left, diam);
int right = height(node->right, diam);
diam = max(diam, left + 2 + right);
return 1 + max(left, right);
}
```