Lintcode93 Balanced Binary Tree solution 题解

来源:互联网 发布:黑社会网络2001 编辑:程序博客网 时间:2024/05/18 01:46

【题目描述】

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。

【题目链接】

www.lintcode.com/en/problem/balanced-binary-tree/

【题目解析】

根据题意,平衡树的定义是两子树的深度差最大不超过1,显然使用递归进行分析较为方便。既然使用递归,那么接下来就需要分析递归调用的终止条件。NULL == root必然是其中一个终止条件,返回0;根据题意还需的另一终止条件应为「左右子树高度差大于1」,但对应此终止条件的返回值是多少?——INT_MAXorINT_MIN?可以传入参数中传入bool指针或者bool引用,并以此变量作为最终返回值。

【参考答案】

www.jiuzhang.com/solutions/balanced-binary-tree/