一、题目地址
https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=188&&tqId=38603&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking
二、具体代码
function getMaxDepth(pRoot) {
if(!pRoot) {
return 0;
}
return Math.max(getMaxDepth(pRoot.left), getMaxDepth(pRoot.right)) + 1;
}
function IsBalanced_Solution(pRoot)
{
if(!pRoot) {
return true;
}
return Math.abs(getMaxDepth(pRoot.left) - getMaxDepth(pRoot.right)) < 2 && IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right);
}
module.exports = {
IsBalanced_Solution : IsBalanced_Solution
};