判断二叉树是否为平衡二叉树

图片说明

/*
 * function TreeNode(x) {
 *   this.val = x;
 *   this.left = null;
 *   this.right = null;
 * }
 */

/**
  * 
  * @param root TreeNode类 
  * @return bool布尔型
  */
function isBalanced( root ) {
    // write code here
    if(!root)return true
    var left_depth = getDeepth(root.left);
    var right_depth = getDeepth(root.right);
    if(Math.abs(left_depth-right_depth)<=1){
        return isBalanced(root.left)&&isBalanced(root.right);
    }else{
        return false;
    }
}
function getDeepth(node){
    if(!node){
        return 0;
    }else{
        return 1 + Math.max(getDeepth(node.left),getDeepth(node.right));
    }
}
module.exports = {
    isBalanced : isBalanced
};
树算法 文章被收录于专栏

树相关算法

全部评论

相关推荐

06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
我的简历长这样
点赞 评论 收藏
分享
05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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