Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth. Solve using recursion (DFS).
Why Interviewers Ask This
Google interviewers use this problem to assess a candidate's fundamental grasp of recursion and tree traversal logic. It evaluates whether you can translate a recursive definition into clean, efficient code without getting lost in edge cases like null nodes or single-node trees. They specifically look for your ability to handle base cases correctly and manage the call stack depth intuitively.
How to Answer This Question
1. Clarify the input: Confirm if the tree is empty (root is null) and define what 'depth' means (number of nodes on the longest path from root to leaf). 2. Define the base case immediately: State that if the node is null, the depth is zero. This prevents infinite recursion. 3. Formulate the recursive step: Explain that the depth of a node is one plus the maximum of its left and right subtree depths. 4. Draft the solution mentally: Visualize the tree structure and how the function unwinds, returning values up the chain. 5. Optimize and discuss complexity: Mention that while this is O(N) time and O(H) space, it is the most readable approach for this specific constraint. 6. Walk through an example: Trace a small tree with three levels to demonstrate the logic flow before writing code.
Key Points to Cover
- Correctly identifying the base case where a null node returns 0
- Using the mathematical formula: 1 + max(left_depth, right_depth)
- Explaining the difference between time complexity O(N) and space complexity O(H)
- Demonstrating clear thought process before writing syntax
- Handling the empty tree edge case explicitly
Sample Answer
To solve this efficiently, I will use a Depth-First Search approach via recursion, which aligns perfectly with the hierarchical nature of binary trees. First, I need to establish my base case. If the current node is null, it contributes zero to the depth, so I return 0 immediately. This handles the edge case of an empty tree or reaching the end of a branch.
Next, for any non-null node, the maximum depth is calculated as one, representing the current node itself, plus the greater of the two depths returned by its left and right children. I don't need to track global variables; instead, I let the recursive calls bubble up the maximum value. For instance, if I have a root with a left child having a depth of 2 and a right child with a depth of 3, the root's depth becomes 1 + max(2, 3), which equals 4.
This approach ensures we visit every node exactly once, resulting in a time complexity of O(N), where N is the number of nodes. The space complexity is O(H), where H is the height of the tree, due to the recursion stack. In the worst-case scenario of a skewed tree, this becomes O(N), but for balanced trees typical in production systems, it remains logarithmic. This method is clean, easy to debug, and demonstrates a solid understanding of recursive problem decomposition, which is highly valued at Google.
Common Mistakes to Avoid
- Returning 1 instead of 0 for the null base case, causing incorrect depth calculations
- Attempting to pass a global counter variable instead of using return values for aggregation
- Confusing maximum depth with minimum depth or failing to take the max of left/right branches
- Neglecting to mention space complexity related to the recursion stack depth
Practice This Question with AI
Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.
Related Interview Questions
Design a Set with $O(1)$ `insert`, `remove`, and `check`
Easy
CiscoFind the Celebrity (Graph)
Easy
UberConvert Binary Tree to Doubly Linked List in Place
Hard
MicrosoftFind K Closest Elements (Heaps)
Medium
MetaDefining Your Own Success Metrics
Medium
GoogleProduct Strategy: Addressing Market Saturation
Medium
Google