Height of a Binary Search Tree

viaGlassdoor

Problem: Given a Binary Search Tree, find its height (the length of the longest path from root to a leaf).

Constraints: Tree can be skewed (worst case O(n) depth) or balanced.

Example: A BST with a single root node has height 1 (or 0, depending on convention); a perfectly balanced BST with n nodes has height O(log n).

Approach: Recursively compute height as 1 + max(height(left subtree), height(right subtree)), with base case height(null) = 0. O(n) time, O(h) space for the recursion stack.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account