BM36 判断是不是平衡二叉树

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function IsBalanced_Solution(pRoot)
{
    // write code here
    function getDepth(root){
        if(root === null) return 0
        let left = getDepth(root.left)
        if(left === -1) return -1
        let right = getDepth(root.right)
        if(right === -1) return -1
        return Math.abs(left-right)>1 ? -1 : 1+Math.max(left,right)

    }
    let L = getDepth(pRoot)
    if(L!== -1){
        return true
    }else{
       return false
    }
}
module.exports = {
    IsBalanced_Solution : IsBalanced_Solution
};

 

posted @ 2022-03-27 22:06  方头小小狮  阅读(25)  评论(0)    收藏  举报