https://leetcode.com/problems/house-robber-iii/

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “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 forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

  3
 / \
2   3
 \   \
  3   1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

    3
   / \ 
  4   5
 / \   \
1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

Recursive function

If we rob a node, then all its children can’t be robbed due to alarm but the children of its children are available to rob.

If we define rob(node) as the max amount of money the thief can get if he robs this tree and node is the root of the tree, then

1
2
3
4
5
6
7
8
9
10
11
12
13
int rob(Node root) {
if (root == null) {
return 0;
}
int max = 0;
if (root.left != null) {
max += rob(root.left.left) + rob(root.left.right);
}
if (root.right != null) {
max += rob(root.right.left) + rob(root.right.right);
}
return Math.max(max + root.val, rob(root.left) + rob(root.right));
}

Dynamic Programming

The above function works. However, there’s overlaps among the functions calls since this is a DFS kind of recursive calls. I can use a cache to store the value to avoid re-computation.

Use a Map to cache the result of node.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private Map<TreeNode, Integer> cache = null;
public int rob(TreeNode root) {
if (cache == null) {
cache = new HashMap<>();
cache.put(null, 0);
}
if (cache.containsKey(root)) {
return cache.get(root);
}
int max = 0;
if (root.left != null) {
max += rob(root.left.left) + rob(root.left.right);
}
if (root.right != null) {
max += rob(root.right.left) + rob(root.right.right);
}
int rst = Math.max(max + root.val, rob(root.left) + rob(root.right));
cache.put(root, rst);
return rst;
}
}

Alternative solution

For a TreeNode root, only 2 possible way to do with it: either rob this root or not.
We can use a int[2] to represent the two scenarios. [0] means the max amount of money we get not to rob the node, [1] means the max money we get if we rob.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int rob(TreeNode root) {
return Math.max(robArr(root)[0], robArr(root)[1]);
}
private int[] robArr(TreeNode root) {
if (root == null) {
return new int[2];
}
int[] left = robArr(root.left);
int[] right = robArr(root.right);
int notRob = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
int rob = root.val + left[0] + right[0];
return new int[]{notRob, rob};
}
}