⌘K
Flycodx

Maximum Depth of Binary Tree

Maximum Depth of Binary Tree

EasyTrees39% accepted

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

7 3 9 20 -1 -1 15 7

Output

3
  • •0 ≤ n ≤ 10^4, -100 ≤ node values ≤ 100
solution.py
Loading editor...
Test 1
Test 2
Test 3

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
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3