Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7] Output: 3
Example 2:
Input: root = [1,null,2] Output: 2
Constraints:
[0, 104].-100 <= Node.val <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are holding an upside-down family tree where every person branches out into more family members. To find the longest path from the top ancestor down to the very last descendant, the brute force strategy checks every single possible path through the entire family tree.
Here's how the algorithm would work step-by-step:
class Solution:
def maxDepth(self, root_node):
if not root_node:
return 0
# We maintain a stack to explore every single path down to the leaves independently
path_stack = [(root_node, 1)]
maximum_depth_found = 0
while path_stack:
current_node, current_depth = path_stack.pop()
if current_node:
maximum_depth_found = max(maximum_depth_found, current_depth)
# We push both children to explore all branches exhaustively
path_stack.append((current_node.left, current_depth + 1))
path_stack.append((current_node.right, current_depth + 1))
return maximum_depth_foundTo find the maximum depth of a family tree represented as connected branches, we can break the problem down by looking at smaller sub-branches. We ask each branch to report the length of its longest path downward, and we use that information to find the absolute deepest path.
Here's how the algorithm would work step-by-step:
def max_depth(tree_node):
# Base case returns zero when the branch terminates without further children.
if tree_node is None:
return 0
left_branch_depth = max_depth(tree_node.left)
right_branch_depth = max_depth(tree_node.right)
# We add one to include the current node in the overall depth calculation.
return max(left_branch_depth, right_branch_depth) + 1| Case | How to Handle |
|---|---|
| Null root pointer representing an empty tree | The base case checks for a null node and immediately returns a depth of 0. |
| A single-node binary tree with no children | Both recursive calls return 0, and the function correctly evaluates 1 plus the maximum of those zeros to return 1. |
| A severely skewed tree resembling a linked list | Recursive DFS solutions will risk a stack overflow if the depth exceeds the call stack limit, requiring an iterative BFS approach for massive inputs. |
| A completely balanced binary tree of maximum constraints | The depth is logarithmic relative to the number of nodes, easily fitting well within standard recursion limits. |
| Nodes containing negative values, zeros, or extreme integer boundaries | Since the algorithm only tracks path length by counting node instances rather than evaluating node values, these values have no impact on the calculation. |
| Unbalanced trees where left and right subtrees have vastly different heights | The max function properly evaluates both branches and selects the greater depth to ensure the longest path is measured. |