C++简洁代码(2行):

平衡二叉树

http://www.nowcoder.com/questionTerminal/8b3b95850edb4115918ecebdf1b4d222

C++简洁代码(2行):

class Solution {
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
        return !pRoot ? true : abs(depth(pRoot->left) - depth(pRoot->right)) <= 1 &&  IsBalanced_Solution(pRoot->left) &&  IsBalanced_Solution(pRoot->right);
    }
    int depth(TreeNode* cur) {//就算二叉树的最大深度
        return !cur ? 0 : max(depth(cur->left), depth(cur->right)) + 1;
    }
};
全部评论
代码虽然简洁,但是里面包含非常多重复的对树的高度的递归
1 回复 分享
发布于 2020-10-07 15:10
复杂都一样复杂, 易读性差了许多
点赞 回复 分享
发布于 2021-03-29 21:59

相关推荐

牛客97567122...:我最近投的几个,都是要不已读不回,要不不回,还有直接拒绝的
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务