110.balanced-binary-tree

2020-05-22  本文已影响0人  Optimization
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;
    }
    
};
上一篇 下一篇

猜你喜欢

热点阅读