题解 | #判断是不是平衡二叉树#【js 实现】

判断是不是平衡二叉树

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

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function IsBalanced_Solution(pRoot)
{
    // write code here
    if(pRoot === null) return true
    const left = getTreeDepth(pRoot.left)
    const right = getTreeDepth(pRoot.right)
    if(Math.abs(left - right) > 1) {
        return false
    }
    const leftTree = IsBalanced_Solution(pRoot.left)
    const rightTree = IsBalanced_Solution(pRoot.right)
    return leftTree && rightTree
        
}
function getTreeDepth(root) {
    if(root === null) return 0
    const left = getTreeDepth(root.left)
    const right = getTreeDepth(root.right)
    return Math.max(left, right) + 1
}
module.exports = {
    IsBalanced_Solution : IsBalanced_Solution
};

全部评论

相关推荐

05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
05-12 11:09
已编辑
门头沟学院 后端
已注销:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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