LeetCode110. Balanced Binary Tree
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.
简单的思路就是计算每个结点左子树和右子树的高度,判断高度差就行了。所以第一个需要的是求高度函数,第二个需要的就是,遍历这颗树。
在进行计算的时候,如果左右子树的高度差满足条件,并不能说明整棵树都是平衡的,还需要检查左子树本身是否平衡,右子树本身是否平衡,形成了递归过程。