题目描述:输入一棵二叉树,判断该二叉树是否是平衡二叉树
function TreeNode(x) {
this.val = x
this.left = null
this.right = null
}
function IsBalanced_Solution(pRoot) {
if(!pRoot) return true
var llen = treeDepth(pRoot.left),
rlen = treeDepth(pRoot.right)
if(Math.abs(llen - rlen) > 1) return false
return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right)
}
function treeDepth(p) {
if(!p) return 0
if(!p.left && !p.right) return 1
return Math.max(treeDepth(p.left), treeDepth(p.right)) + 1
}