Graph Theory37 sections · 1633 units
Open in Course

LeetCode 337 House Robber III - Base Case

Null nodes return zero

If vv is null (no node exists), both states return 00. You cannot rob a house that does not exist.

if v is null then
    return (0, 0)

This base case handles leaf nodes' children and empty subtrees. From here, you build up: leaves compute their DP from null children, parents compute from children, and so on up to the root. The final answer is max(dp[root][0], dp[root][1]).