. The diameter of a binary tree is the length of the longest path between any two nodes in a tree.
Solution: (Using Depth and Storing the possible diameter)
class Solution
{
public:
int res = 0;
int findDiameter(TreeNode *root)
{
if (root == NULL)
{
return 0;
}
int ld = findDiameter(root->left);
int rd = findDiameter(root->right);
res = max(res, ld + rd);
return max(ld, rd) + 1;
}
int diameterOfBinaryTree(TreeNode *root)
{
if (root == NULL)
{
return 0;
}
findDiameter(root);
return res;
}
};