Maximum Depth of Binary Tree
Maximum Depth of Binary Tree
Given a binary tree in level-order representation, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root down to the farthest leaf node. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: level-order [3 9 20 -1 -1 15 7] → depth 3
Input format — only if your code reads from stdin
Input format (stdin): - First line: integer n (size of level-order array) - Second line: n space-separated integers, where -1 represents a null node Output: a single integer — the maximum depth of the tree.
Approach
Recursive DFS: maxDepth(node) = 1 + max(maxDepth(left), maxDepth(right)). Base case: maxDepth(null) = 0. Alternatively BFS level by level, counting levels.
Input
Output
- •
0 ≤ n ≤ 10^4, -100 ≤ node values ≤ 100
More Trees problems
Browse all coding problems →Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards