1. 题目
JZ79 判断是不是平衡二叉树
2. 代码实现
function IsBalanced_Solution(pRoot)
{
if(pRoot === null) return true
const left = getTreeDepth(pRoot.left)
const right = getTreeDepth(pRoot.right)
if(Math.abs(left - right) > 1) {
return false
}
const leftTree = IsBalanced_Solution(pRoot.left)
const rightTree = IsBalanced_Solution(pRoot.right)
return leftTree && rightTree
}
function getTreeDepth(root) {
if(root === null) return 0
const left = getTreeDepth(root.left)
const right = getTreeDepth(root.right)
return Math.max(left, right) + 1
}
module.exports = {
IsBalanced_Solution : IsBalanced_Solution
};