classNode{constructor(value){this.value = value
this.left =nullthis.right =null}}let a =newNode('a')let b =newNode('b')let c =newNode('c')let d =newNode('d')let e =newNode('e')let f =newNode('f')let g =newNode('g')let h =newNode('h')let i =newNode('i')
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
c.right = g
d.left = h
e.right = i // true// h.left = i // false// 获取二叉树的深度functiongetDeep(root){if(root ===null)return0let leftDeep =getDeep(root.left)let rightDeep =getDeep(root.right)return Math.max(leftDeep, rightDeep)+1}functionisBalanced(root){if(root ===null)returntruelet leftDeep =getDeep(root.left)let rightDeep =getDeep(root.right)// 如果左右子树的高度差大于1,则不是平衡二叉树if(Math.abs(leftDeep - rightDeep)>1)returnfalse// 如果左右子树的高度差小于等于1,则继续判断左右子树是否是平衡二叉树returnisBalanced(root.left)&&isBalanced(root.right)}
console.log(isBalanced(a))