// https://leetcode-cn.com/problems/balanced-binary-tree/
// 一棵高度平衡二叉樹定義為:
// 一個(gè)二叉樹每個(gè)節(jié)點(diǎn) 的左右兩個(gè)子樹的高度差的絕對(duì)值不超過(guò) 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;
};