Difficulty: Medium, Asked-In: Amazon, VMWare, Zoho.
Key takeaway: A fundamental tree problem to learn problem-solving using DFS and BFS traversal.
Given a binary tree, write a program to find its height. In other words, we need to calculate the maximum depth of the binary tree.

In the above diagram, 5, 8, and 9 are leaf nodes.
The maximum count of edges from root to farthest leaf = max(3, 3, 2) = 3.
So, the height of the given binary tree = 3.
A binary tree is a recursive structure where we can solve the problem using the solution of two smaller subproblems: the left sub-tree and the right sub-tree. So how can we apply this idea to find the height of a binary tree? Let's think!
For finding the height, we should know the path length of the root to the deepest leaf node, which can be either present in the left-subtree or right sub-tree. In other words, the longest path from the root to the deepest leaf can go through either the left or right child of the root.
One idea would be to calculate the height of the left and right sub-tree recursively. Whichever sub-tree has the max height, we take that value and add 1 to get the height of the tree. The height of the tree = The longest path from the root to the leaf node = 1 + max (height of the left subtree, height of the right subtree). Before calculating the height of the tree, we need to calculate the height of the left and right sub-trees. So this is the idea similar to post-order traversal!
Suppose we are using the function treeHeight(TreeNode root).
treeHeight(root)
if root is null or root is a leaf
return 0
leftHeight = treeHeight(root.left)
rightHeight = treeHeight(root.right)
if leftHeight > rightHeight
return leftHeight + 1
else
return rightHeight + 1We are visiting each node using post-order and doing O(1) operation with each node. So time complexity = n*O(1) = O(n). Space complexity = Size of the recursion call stack = O(h).
Now, a critical question is: Can we find the height of the tree without recursion? Can we use level-order traversal? As we know, the height of the binary tree = Total number of levels - 1. So one idea is: traverse the tree using level order, count the number of levels and calculate the height.
Here we traverse the tree level by level and increment a level count by 1 whenever we reach a new level. So how do we identify the situation when we reach a new level? Here is the idea: Suppose level order traversal is at the start of some level x. Now after processing all the nodes at level x, we will reach the new level x + 1.
To implement this, we count the nodes at the current level x and, using a loop, process all the nodes at the current level and add all next level nodes to the queue. Now all current-level nodes are removed from the queue, and next level nodes are inside the queue. It means: We are at the start of the next level x + 1. So at this stage, we increment the level count by 1. We keep repeating this process whenever we encounter a new level.
Run a loop till Q becomes empty. One iteration of this loop will explore one level. So at each iteration:
treeHeight(root)
if root is null or root is a leaf
return 0
levelCount = 0
queue = empty queue
enqueue root
while queue is not empty
levelCount = levelCount + 1
nodeCount = size of queue
// Process all nodes at the current level
for i from 0 to nodeCount - 1
node = dequeue queue
if node.left is not null
enqueue node.left
if node.right is not null
enqueue node.right
// Height is measured in edges
// so subtract one from the level count
return levelCount - 1Time complexity = Time complexity of the level order traversal = O(n).
Space complexity = Space complexity of the level order traversal.
treeHeight(root)
if root is null
return 0
queue = empty queue
enqueue root
enqueue null // Marks the end of the current level
height = 0
while queue is not empty
node = dequeue queue
if node is null
// Finished processing one level
height = height + 1
// Mark the end of the next level, if nodes remain
if queue is not empty
enqueue null
else
if node.left is not null
enqueue node.left
if node.right is not null
enqueue node.right
return heightEnjoy learning! Enjoy algorithms!