判断是否是平衡二叉树

图片说明

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function IsBalanced_Solution(pRoot)
{
    // write code here
    if(!pRoot) return true
    function dfs(root){
        if(!root) return 0
        return Math.max(dfs(root.left),dfs(root.right))+1
    }
    let left = dfs(pRoot.left)
    let right = dfs(pRoot.right)
    if(left-right>1 || right-left >1) return false
    return true
}
module.exports = {
    IsBalanced_Solution : IsBalanced_Solution
};
树算法 文章被收录于专栏

树相关算法

全部评论

相关推荐

仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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