LeetCode力扣 110. 平衡二叉树 Balanced Binary Tree 题解代码 JavaScript

问题 https://leetcode-cn.com/problems/balanced-binary-tree/

练习使用JavaScript解答

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var bala = true;

function funBala(root) {
    if(!root)
        return 0;
    var l = funBala(root.left);
    var r = funBala(root.right);
    if(Math.abs(l-r) > 1)
        bala = false;
    return Math.max(l,r)+1;
}
var isBalanced = function(root) {
    bala = true;
    funBala(root);
    return bala;
};

 

你可能感兴趣的:(LeetCode题解)