int mxheight(Node* root)
{
if(root==NULL) return 0;
return max(mxheight(root->left), mxheight(root->right))+1;
}
class Solution {
public:
// Function to return the diameter of a Binary Tree.
int diameter(Node* root) {
// Your code here
if(root==NULL) return 0;
int p=mxheight(root->left)+mxheight(root->right)+1;
return max(p, max(diameter(root->left), diameter(root->right)));
}
};
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter