House Robber III
Question (LC.337)
Analysis
Approach
1. define optimal subproblems - by subtree
rob(root) = maximum amount of money you can rob at the subtree starting from root
2. solve by subtree, again 2 options, to rob at root or skip it
rob(root) = Math.max(root.value + rob(root.left.left) + rob(root.left.right) + rob(root.right.left)
+ rob(root.right.right), rob(root.left) + rob(root.right));
3. topo order
from leaf nodes to the root
4. init base cases
if (root == null) return 0
5. answer
return memo.get(root)Code
Time & Space Complexity
Follow Up #3
Analysis
Updated Code
Time & Space Complexity
Conclusion
Last updated