LintCode 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.

Example

Given binary tree A = {3,9,20,#,#,15,7}, B = {3,#,20,15,7}

1 A)  3            B)    3 
2    / \                  \
3   9  20                 20
4     /  \                / \
5    15   7              15  7

The binary tree A is a height-balanced binary tree, but B is not.

 

For this problem I am thinking to use recursive method to deal with it.

1. The banlance of the tree need to be made sure with the balance of the left tree and right tree. Then the height of right tree and left tree difference should not be larger than one.

2. The problem size is reducing.

3. When the tree is null or is leave base case is done. It is banlanced and height is 0 or 1;

posted on 2016-08-08 08:42  猛犸猴子  阅读(112)  评论(0)    收藏  举报

导航