Recursively compute maximum depth: 1 + max(maxDepth(root.left), maxDepth(root.right)), returning 0 for null nodes.
def maxDepth(root):
# Base Case: If node is null, depth is 0
if not root:
return 0
# Recursive calls for left and right subtrees
left = maxDepth(root.left)
right = maxDepth(root.right)
# Max depth is 1 + max of children
return 1 + max(left, right)
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}