5. Graphs & Dynamic Programming

Practice Lab 2: Trees, Shortest Grid Path & Dynamic Programming

Solve three classic senior interview problems in the live Python sandbox: Lowest Common Ancestor / Path Sum, Shortest Path in a Binary Grid (BFS), and Climbing Stairs with Costs (DP).

5 min readUpdated 2026-11-30
In This Lesson
  • Check root-to-leaf target sums recursively on binary trees
  • Find shortest grid paths cleanly with BFS and a visited set
  • Solve 1D minimum-cost path problems with O(1)-space Dynamic Programming

Put Modules 4 and 5 together in this coding interview lab! Each drill targets a distinct core paradigm—recursive tree DFS, unweighted shortest-path grid BFS, and O(1)-space 1D Dynamic Programming.

Blueprint & Edge-Case Guide Before You Code

DrillParadigmTarget ComplexityClassic Bug to Avoid
1. has_path_sumBinary Tree DFSO(n) time, O(h) stackReturning True at a non-leaf node that only has one child!
2. shortest_grid_pathGrid BFS (deque)O(R · C) time & spaceForgetting to check if start (0, 0) or goal (R-1, C-1) is a wall (1)!
3. min_cost_stairsBottom-Up 1D DPO(n) time, O(1) spaceOff-by-one at the top step (you must step past the last index, taking min(a, b))!

Walkthrough Notes for Each Drill

  1. Drill 1 (has_path_sum):
    • A leaf has neither left nor right child (not root.left and not root.right). Only at a leaf should you check target == root.val.
    • Note: Node values can be negative, so do not prune early when rem < 0!
  2. Drill 2 (shortest_grid_path):
    • Start at (0, 0) with path cell count 1, marking (0, 0) in visited immediately!
    • Guard 1 × 1 grids ([[0]] returns 1, [[1]] returns -1) and blocked start/end cells upfront.
    • Trace on [[0, 0, 0], [1, 1, 0], [0, 0, 0]]: (0,0):1 → (0,1):2 → (0,2):3 → (1,2):4 → (2,2):5 → returns 5!
  3. Drill 3 (min_cost_stairs):
    • You can start at index 0 or index 1 for free, and pay cost[i] whenever you step from index i (moving +1 or +2).
    • Trace on cost = [10, 15, 20] (a = 0, b = 0):
      • c = 10: a, b = 0, 10 + min(0, 0) = 10
      • c = 15: a, b = 10, 15 + min(0, 10) = 15
      • c = 20: a, b = 15, 20 + min(10, 15) = 30
      • Past the top: min(a, b) = min(15, 30) = 15!
Do Not Check target == 0 on None in has_path_sum

If a root 1 has a left child 2 and right is None, calling has_path_sum(root.right, 0) on None with if not root: return target == 0 would falsely say True for target = 1—even though 1 is NOT a leaf! Always check if not root: return False, and test target == root.val only when not root.left and not root.right.

Enqueue Distance Directly Inside the BFS Tuple

Storing (r, c, dist) inside the deque keeps shortest_grid_path concise and removes the need for a separate level-size inner loop.

Live Coding Lab

Implement all three functions in the sandbox below and uncomment the automated assert checks to verify your solutions:

Python 3Lab 2: Trees, Grid BFS & DP Drills
Output

Click “Run Code” to execute this snippet right in your browser.

Lab Checkpoint

Check Your Understanding

In has_path_sum(TreeNode(1, TreeNode(2)), 1), why must the function return False?

DSA Lab 2 Checklist

0/3 done