Challenge 4: Finding the Height of a Binary Tree
Given the root to a binary search tree, write a function to find the height of the tree.
We'll cover the following
Problem statement
Implement a function int findHeight(Node* rootNode)
, which returns the height of a given binary search tree.
- 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 the leaf nodes are zero.
Input
This is the root node.
Output
It 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 80+ hands-on prep courses.