Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
利用求最大深度,然后再求两边相减的绝对值如果是小于1的就行,然后如果所有子树都是平衡二叉树那么大树那必然是平衡二叉树。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
pub

这篇博客主要探讨如何判断一个二叉树是否为高度平衡的。平衡二叉树定义为每个节点的两子树深度最大差不超过1。作者分享了一种通过计算子树最大深度并比较的方法,并提供了简洁的代码实现,特别是关键的判断条件:如果左右子树深度差的绝对值大于1,则树不平衡。
订阅专栏 解锁全文
552

被折叠的 条评论
为什么被折叠?



