# Leetcode 337 - House robber III

Note

• DFS
• Normal recursion will TLE. We need a map memo to remember the max result for node.
• DP
• When it comes to tree, when need recursion in DP.
• We need an array to remember the states of children.
• In this case, we need an array of length 2. res[0] contains the value when we don’t pick the current node, while res[1] contains the value when we pick the current node.

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root.

Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.

Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police.

Example

Iterative

DP