leetcode-110-是否是平衡二叉树

// 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;
};

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。