parent
08e226bb00
commit
f1a3fbc788
|
|
@ -614,8 +614,10 @@ var isBalanced = function(root) {
|
||||||
if(node === null) return 0;
|
if(node === null) return 0;
|
||||||
// 3. 确定单层递归逻辑
|
// 3. 确定单层递归逻辑
|
||||||
let leftDepth = getDepth(node.left); //左子树高度
|
let leftDepth = getDepth(node.left); //左子树高度
|
||||||
let rightDepth = getDepth(node.right); //右子树高度
|
// 当判定左子树不为平衡二叉树时,即可直接返回-1
|
||||||
if(leftDepth === -1) return -1;
|
if(leftDepth === -1) return -1;
|
||||||
|
let rightDepth = getDepth(node.right); //右子树高度
|
||||||
|
// 当判定右子树不为平衡二叉树时,即可直接返回-1
|
||||||
if(rightDepth === -1) return -1;
|
if(rightDepth === -1) return -1;
|
||||||
if(Math.abs(leftDepth - rightDepth) > 1) {
|
if(Math.abs(leftDepth - rightDepth) > 1) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue