class Solution {
public:
int d = 0;
int max(int a, int b)
{
return a > b ? a : b;
}
int diameterOfBinaryTree(TreeNode* root) {
depth(root);
return d;
}
int depth(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
int l = depth(root->left);
int r = depth(root->right);
d = max(d, l + r);
return 1 + max(l, r);
}
};
LeetCode543. 二叉树的直径
最新推荐文章于 2022-06-06 16:07:21 发布