题解 | #判断是不是平衡二叉树#

判断是不是平衡二叉树

https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
// 用来标识
var flag = true;
//先求深度
function deep(root){
    if(root == null){
        return 0;
    }
    let left = deep(root.left);
    let right = deep(root.right);
    if(Math.abs(left-right) > 1){
        flag = false;
    }
    return Math.max(left,right) + 1;
}
function IsBalanced_Solution(pRoot)
{
    // write code here
    if(pRoot == null){
        return true;
    }
    // let left = IsBalanced_Solution(pRoot.left);
    // let right = IsBalanced_Solution(pRoot.right);
    // if(Math.abs(left-right) > 1){
    //     return false;
    // }
    deep(pRoot);
    return flag;
}
module.exports = {
    IsBalanced_Solution : IsBalanced_Solution
};

#我的实习求职记录#
全部评论

相关推荐

只写bug的程序媛:人家说一本以上,不是及以上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务