/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int largeheight(TreeNode* root)
{
if(!root) return 0;
return max(largeheight(root->left), largeheight(root->right))+1;
}
int diameterOfBinaryTree(TreeNode* root) {
if(!root) return 0;
int h1=largeheight(root->left);
int h2=largeheight(root->right);
int p1=h1+h2;
return max(p1, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(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