```
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
depth(root);
return maxVal;
}
int maxVal = 0;
int depth(TreeNode* tree) {
if(tree == nullptr)
return 0;
int left = 0, right = 0;
if(tree->left)
left = depth(tree->left)+1;
if(tree->right)
right = depth(tree->right)+1;
int total = left+right;
maxVal = max(maxVal,total);
return max(left,right);
}
};
```