Challenge: Find the Height of a BST

Given the root to a Binary Search Tree, write a function to find the height of the tree. A solution is placed in the "solution" section for your help, but we would suggest you to solve it on your own first.

Problem Statement #

Implement a function findHeight(root) which returns the height of a given binary search tree. An illustration is also provided for your understanding.

  • Height of a Node — the number of edges between a node and its deepest descendent
  • Height of a Tree — Height of its root node

Also, keep in mind that the height of an empty tree and leaf nodes is zero.

Output #

Returns the maximum depth or height of a binary tree

Sample Input #

bst = {
		6 -> 4,9
    4 -> 2,5
    9 -> 8,12
    12 -> 10,14
}
where parent -> leftChild,rightChild

Sample Output #

3

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.