力扣第543题 二叉树的直径
/**
* 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 res;
int func(TreeNode* root)
{
if (root == NULL)
return 0;
int left = func(root->left);
int right = func(root->right);
res = max<int>(res, left + right + 1);
return max<int>(left, right) + 1;
}
int diameterOfBinaryTree(TreeNode* root)
{
res = 1;
func(root);
return res - 1;
}
};