美文网首页
110.balanced-binary-tree

110.balanced-binary-tree

作者: Optimization | 来源:发表于2020-05-22 14:42 被阅读0次
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        // 到底卡在哪
        // 回忆一下过去的
        // 判断这个树的深度,判断另一棵树的深度
        if(!root) return true;
        // 难点 1
        // if(!root->left || !root->right) return true;
        // 难点 2
        return (abs(height(root->left)-height(root->right)) <= 1)&&isBalanced(root->left)&&isBalanced(root->right);
    }
private:
    int height(TreeNode* root){
        if(!root) return 0;
        // +1 需要用例子来弄!难点 3
        return max(height(root->left), height(root->right)) + 1;
    }
    
};

网友评论

      本文标题:110.balanced-binary-tree

      本文链接:https://www.haomeiwen.com/subject/sumqahtx.html