VCC Monthly Coding Challenge - Tree: Height of a Binary Tree

Challenge

The height of a binary tree is the number of edges between the tree’s root and its furthest leaf. This means that a tree containing a single node has a height of .

Complete a function so that it returns the height of a binary tree. This function has a parameter, which is a pointer to the root node of a binary tree.

Note -The Height of binary tree with single node is taken as zero.

Input Format

You should read utf-8 input from stdin. This input will pass the root node of a binary tree to your function.

Output Format

Your function should return a single integer denoting the height of the binary tree.

Sample Input

Note: A binary search tree is a binary tree in which the value of each parent node’s left child is less than the value the parent node, and the value of the parent node is less than the value of its right child.

Sample Output

3

Explanation

The longest root-to-leaf path is shown below:

There are 4 nodes in this path that are connected by 3 edges, meaning our binary tree’s height = 3. Thus, we print 3 as our answer.

Challenge Rules

  1. Any language.
  2. Post the code in cloud9, pastebin, or github with a link here.
  3. Winner will be announced at the January VCC Committee Meeting.
  4. Bonus points are awarded for most efficient algorithm, lease LOC, and documentation