美文网首页
110 balanced binary tree

110 balanced binary tree

作者: larrymusk | 来源:发表于2017-11-20 13:20 被阅读0次

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

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