Open pwstrick opened 4 years ago
110. 平衡二叉树
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {boolean} */ var isBalanced = function(root) { if(root == null) return true; if(Math.abs(height(root.left) - height(root.right)) > 1) { return false; } return isBalanced(root.left) && isBalanced(root.right) }; function height(root) { if(root == null) return 0; return Math.max.call(undefined, height(root.left) + 1, height(root.right) + 1); }
110. 平衡二叉树