/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getDepth(TreeNode* root)
{
if(root == NULL)
return 0;
int leftD = getDepth(root>left);
int rightD = getDepth(root>right);
return leftD > rightD ? (leftD + 1) : (rightD + 1);
}
int diameterOfBinaryTree(TreeNode* root) {
if(root == NULL) return 0;
return getDepth(root>left) + getDepth(root>right);
}
};
Why my solution didn't work


See here, case 2.
Your code assume the longest path will include the root node. If not, it tells wrong answer.