LeetCode 543:二叉树的直径

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:24   1079   0
class Solution {
public:
    int ans = 0;

    int diameterOfBinaryTree(TreeNode* root) {
        //ans = 1;
        dfs(root);
        return ans;
    }

    int dfs(TreeNode* root) {
        if(!root) return 0;
        int l = dfs(root->left);
        int r = dfs(root->right);
        ans = max(ans, l + r);
        return max(l, r) + 1;
    }
};

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP