Find the maximum depth (height) of a binary tree. Depth is defined as the number of nodes along the longest path from root to any leaf.
Problem Statement
Given a binary tree, find the maximum depth. A leaf node's depth is 1. An empty tree has depth 0.
Example: Tree [3,9,20,null,null,15,7] has depth 3.
Approach
Complete C# Solution
// LeetCode 104 public class Solution { public int MaxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.Max(MaxDepth(root.left), MaxDepth(root.right)); } }
Complexity Analysis
h = height; recursion stack depth equals tree height. Balanced tree: O(log n), skewed: O(n).
Edge Cases
- Null tree: returns 0
- Single node: returns 1
- Skewed tree (linked list): O(n) space used