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.
For example:
Input: root = [3,9,20,null,null,15,7] Output: 3
In this example, the tree looks like this:
3
/ \
9 20
/ \
15 7
Another example:
Input: root = [1,null,2] Output: 2
In this case, the tree is:
1 \ 2
Could you provide an algorithm to find the maximum depth of a binary tree? What is the time and space complexity of your approach? Are there any edge cases to consider? Please explain your reasoning.
When 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:
The goal is to find how deep a tree is by checking every path from the top to the bottom. We will explore each possible route to find the longest one. Think of it as trying every single road in a maze to see which one is the longest.
Here's how the algorithm would work step-by-step:
def maximum_depth_of_binary_tree_brute_force(root):
maximum_depth = 0
def calculate_depth(node, current_depth):
nonlocal maximum_depth
# If we've hit a leaf, compare this path's depth.
if node is None:
maximum_depth = max(maximum_depth, current_depth)
return
# Traverse the left subtree.
calculate_depth(node.left, current_depth + 1)
# Traverse the right subtree.
calculate_depth(node.right, current_depth + 1)
# Initiate the recursive depth calculation.
calculate_depth(root, 0)
return maximum_depth
The best way to find the maximum depth of a binary tree is to explore it level by level. We use a method where we go as deep as possible on each side and keep track of the deepest point we reach.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def max_depth_binary_tree(root):
if root is None:
return 0
# Recursively calculate the depth of the left subtree
left_subtree_depth = max_depth_binary_tree(root.left)
# Recursively calculate the depth of the right subtree
right_subtree_depth = max_depth_binary_tree(root.right)
# Return the maximum depth of either subtree, plus 1 for the current node.
return max(left_subtree_depth, right_subtree_depth) + 1
Case | How to Handle |
---|---|
Null or empty root node | If the root is null, the maximum depth is 0, return 0 to handle the empty tree case. |
Single node tree (only root) | If the root has no children, the maximum depth is 1, as it has one node in the path. |
Completely unbalanced tree (left or right skew) | The recursive or iterative DFS/BFS will traverse the entire skewed path, correctly determining the depth. |
Large tree (high depth) | The recursive approach might hit stack overflow limits for extreme depths, making an iterative BFS approach preferable for very deep trees. |
Tree with negative node values (though not depth-related, confirms general handling) | Node values do not affect depth calculation; algorithm works regardless of node value signs. |
Maximum integer values for node values (tests potential numerical issues, not depth related) | Depth calculation does not use node values, so Integer.MAX_VALUE values will not influence depth result. |
Perfectly balanced tree | The algorithm should correctly and efficiently calculate the depth of a perfectly balanced tree due to the uniform depth of all paths. |
Binary tree with duplicate depths (different paths of same length) | The algorithm finds the *maximum* depth, so multiple paths of the same maximum length are irrelevant. |