Workspace Maximum Depth Of Binary Tree
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Depth of a Complete Binary Tree visualizer

Return the maximum number of nodes along a root-to-leaf path in a complete binary tree encoded in level order.

EXECUTION LAB

Return the maximum number of nodes along a root-to-leaf path in a complete binary tree encoded in level order.

Input and constraints
  • Use at most 32 integers. Each numeric input is between -1000 and 1000.

Code inspector

def solve(values, target):
node = 0
depth = 0
while (node < len(values)):
depth = (depth + 1)
node = ((node * 2) + 1)
return depth

Visualizer

Step 1 of 4
8active4916

Count the level at node the current value

Count the level at node 0.

Variable watch

node 0depth 0

Step explanation

Count the level at node 0.

comparisons 1reads 0writes 0swaps 0recursiveCalls 0
Educational simulation · O(log(n + 1)) time · O(1) space

Worked explanation

On a full row of seats filled left to right, the left aisle reaches every occupied row.

Guided solution

In a complete tree the leftmost path reaches the deepest occupied level.

O(log(n + 1)) time · O(1) auxiliary space

Examples

Worked example · {"values":[8,4,9,1,6],"target":0} ⇒ 3

Edge case 1 · {"values":[],"target":0} ⇒ 0

Edge case 2 · {"values":[7],"target":0} ⇒ 1

Progressive hints

Hint 1On a full row of seats filled left to right, the left aisle reaches every occupied row.
Hint 2In a complete tree the leftmost path reaches the deepest occupied level.
Hint 3Step through the worked example and inspect the highlighted variables before each update.

Common mistake

Following only left links would be incorrect for a general sparse tree.

Interview follow-up

Which traversal would compute depth in a sparse tree?

Continue learning

Practice related DSA questions in the same topic.