binary tree
recursion
algorithm
data structures
node height

how to find the height of a node in binary tree recursively

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The height of a node in a binary tree is defined as the number of edges on the longest path from that node to a leaf node. A leaf node is a node without any children. Determining the height of a node is a fundamental operation in various tree-related algorithms and structures. Recursively calculating the height of a node is one of the most intuitive approaches. In this article, we will explore the recursive method for finding the height of a binary tree node, along with technical explanations and examples.

Understanding Recursion in Binary Trees

Recursion is a natural fit for problems involving trees due to their hierarchical structure. By breaking down a problem into smaller sub-problems that mirror the larger problem, recursion provides a clear and concise solution.

Recursive Formula

For a given node N, the height can be computed as follows:

  • If N is null, the height is -1, following the convention that an empty tree has a height of -1.
  • If N is a leaf node, the height is 0.
  • Otherwise, the height of N is 1 + max(height of left subtree, height of right subtree).

Recursive Pseudocode

Below is a pseudocode representation of the recursive algorithm to find the height of a node in a binary tree.

plaintext
1function findHeight(node):
2  if node is null:
3    return -1
4  else:
5    leftHeight = findHeight(node.left)
6    rightHeight = findHeight(node.right)
7    return 1 + max(leftHeight, rightHeight)

Example

Consider the binary tree below:

 
1         A
2       /   \
3      B     C
4     / \   / \
5    D   E F   G

To find the height of the root node A, apply the recursive formula:

  1. Calculate the height of node B:
    • For B, calculate the height of D and E, both of which are 0 since they are leaf nodes.
    • Thus, height(B) = 1 + max(0, 0) = 1.
  2. Calculate the height of node C:
    • For C, calculate the height of F and G, both of which are 0.
    • Thus, height(C) = 1 + max(0, 0) = 1.
  3. Finally, compute the height of node A:
    • height(A) = 1 + max(1, 1) = 2.

Complexity Analysis

The time complexity of finding the height of a node in a binary tree recursively is O(n)O(n), where nn is the number of nodes in the binary tree. This complexity arises because we potentially visit each node once in the tree.

The space complexity is O(h)O(h), where hh is the height of the tree, due to the space consumed by the recursive call stack. In the worst case, this could be O(n)O(n) if the tree is skewed.

Key Points Summary

Key PointDescription
DefinitionNumber of edges on the longest path to a leaf.
Base CaseHeight of null node is -1.
Recursive Case1+max(height(left),height(right))1 + \max(\text{height(left)}, \text{height(right)}).
ComplexityTime: O(n)O(n) Space: O(h)O(h)
Example Tree HeightHeight of tree with root A is 2.

Additional Considerations

  • Balancing Trees: In balanced trees, the height is logarithmic relative to the number of nodes, i.e., O(log(n))O(\log(n)). This significantly affects the space complexity in such scenarios.
  • Applications: Understanding node height is critical for AVL trees, Red-Black trees, and other self-balancing binary search trees to maintain their invariants and properties.
  • Iterative Method: An iterative approach using a queue to perform a level-order traversal can also compute the height, although it's generally less straightforward than recursion for this task.

Conclusion

Finding the height of a node recursively in a binary tree is an essential skill, with applications extending into various domains like data structures and algorithms optimization. The simplicity and elegance of recursive solutions often align well with the recursive nature of trees, providing both an intuitive and efficient method for computing node heights. Understanding the underlying mechanics and complexities involved will allow for more robust and efficient software design.


Course illustration
Course illustration

All Rights Reserved.