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.
Basically, to find the height of a binary tree (the maximum distance from root node to leaf node):
- Find the height of the left child/subtree and right child/subtree
- Return the maximum between the two subtrees.
- Add 1 to it (because 1 is the height of the current parent node)
- Do this recursively as each node may have children.