题解 | #判断是不是平衡二叉树#【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
};

全部评论

相关推荐

看到这个内容真是闹麻了。。。。。。现在有了AI以后很多人面试都会作弊吗? 那对老老实实面试的人岂不是不公平....
程序员牛肉:公平那是对小孩子讲的童话故事,成年人的世界只有能不能接受失败的后果。 你要是能接受面试作弊被发现之后多家公司联合永久拉黑的后果,你就搞。
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
买蜜雪也用卷:我觉得应该没有哪个人敢说自己熟练使用git,代码分支一复杂还是得慢慢寻思一下的,不过基本的拉代码提交代码还有分支什么的是应该会
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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