leetcode-110-是否是平衡二叉树
2021-03-29 本文已影响0人
kayleeWei
// https://leetcode-cn.com/problems/balanced-binary-tree/
// 一棵高度平衡二叉树定义为:
// 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
function balanced (node) {
if (!node) return 0;
const leftHeight = balanced(node.left);
const rightHeight = balanced(node.right);
if (leftHeight === -1 || rightHeight === - 1 || Math.abs(rightHeight - leftHeight) > 1) {
return -1;
} else {
return Math.max(leftHeight, rightHeight) + 1;
}
}
var isBalanced = function(root) {
return balanced(root) !== -1;
};