Balanced Binary Tree

Posted on

Problem:

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.

Solution:

Screen Shot 2015-10-27 at 2.36.51 PM

Through each part of the solution, use recursion. Every node acts just like root. Be clear about the balanced conditions:

  1. Both left and right child nodes are balanced
  2. The height difference between the child nodes <= 1

So write another helper function to count the height. Use the same recursion idea to count the height for any node. For any node, the total height = larger child node height + 1.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s