110 balanced binary tree

1,判断当前左右树的最大高度差是不是小于等于一
2,是的话继续判断,左右子树是否满足上面条件

#define max(a,b) (a>b?a:b)

int depth(struct TreeNode *root){
    
    if(root == NULL)
        return 0;
    int l = depth(root->left);
    int r = depth(root->right);
    
    return max(l,r)+1;

}

bool isBalanced(struct TreeNode* root) {
    if(root == NULL)
        return true;
    if(abs(depth(root->left)-depth(root->right)) > 1)
        return false;

    return isBalanced(root->left)&&isBalanced(root->right);
}



你可能感兴趣的:(110 balanced binary tree)