1. var isBalanced = function (root) {
    2. let res = true
    3. // 计算左右子树的最大深度
    4. const maxDepth = (root) => {
    5. if (!root) return 0
    6. const r = maxDepth(root.right)
    7. const l = maxDepth(root.left)
    8. if (Math.abs(r - l) > 1) {
    9. res = false
    10. }
    11. return Math.max(r, l) + 1
    12. }
    13. maxDepth(root)
    14. return res
    15. };