Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Concept

Passing Values Down and Up

Parameters go down, returns come up
step 1 / 18
binary tree
4
2
1
3
5
call stack ↓
(returned)
Concept
1walk(node, depth): // depth PASSED DOWN ↓
2 push frame
3 if node is null: return depth-1 // base case
4 return max(depth, // RETURN max depth UP ↑
5 walk(node.left, depth+1),
6 walk(node.right, depth+1))
state
  • ↓ paramdepth
  • ↑ returnmax depth

line 1Recursion moves data in TWO directions. Parameters carry context DOWN from parent to child; return values carry answers UP from child to parent. We will compute each node's depth (passed DOWN) and the tree's maximum depth (returned UP) in a single walk.