Hackerrank Day 22: Binary Search Trees

screen-shot-2016-12-29-at-3-06-56-pmscreen-shot-2016-12-29-at-3-07-05-pm

My Solution in Ruby: 

The Hacker Rank’s Tutorial was actually helpful in solving this.   It gave me the formula that’s essentially the solution to this challenge.

screen-shot-2016-12-29-at-3-09-04-pm

Basically, to find the height of a binary tree (the maximum distance from root node to leaf node):

  1. Find the height of the left child/subtree and right child/subtree
  2. Return the maximum between the two subtrees.
  3. Add 1 to it (because 1 is the height of the current parent node)
  4. Do this recursively as each node may have children.

screen-shot-2016-12-29-at-3-06-30-pm

Advertisements
Hackerrank Day 22: Binary Search Trees

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