
Problem
Solution
Submissions
Maximum Depth of a Binary Tree
Certification: Advanced Level
Accuracy: 100%
Submissions: 1
Points: 8
p>Write a C# function to find the maximum depth of a binary tree. The 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
- Explanation:
- The tree has 3 levels:
- The tree has 3 levels:
Example 2
- Input: root = [1, null, 2]
- Output: 2
- Explanation:
- The tree has 2 levels:
- The tree has 2 levels:
Constraints
- The number of nodes in the tree is in the range [0, 10^4]
- -100 ≤ Node.val ≤ 100
- Time Complexity: O(n)
- Space Complexity: O(h) where h is the height of the tree
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a recursive depth-first search (DFS) approach
- For each node, calculate the maximum depth of its left and right subtrees
- The maximum depth at any node is 1 (the node itself) plus the maximum of the depths of its subtrees
- Handle the base case where a node is null (depth = 0)