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:
按照定义,balanced是指left tree和right tree的max height的差值可以小于等于1,同时要满足left tree和right tree都是一个balanced tree。
用返回一个resultSet的办法返回多值,(balanced,maxHeight). 要注意的是返回的时候root的height需要是max(leftHeight, rightHeight) + 1
C++
Java
解法2: O(N), 不需要mark node
直接用一个返回值来区分是否是balanced binary tree。
|
|