友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

337. 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:
Input: [3,2,3,null,3,null,1]

     3
    / \
   2   3
    \   \
     3   1

Output: 7
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
Input: [3,4,5,1,3,null,1]

     3
    / \
   4   5
  / \   \
 1   3   1

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

解题分析

由于题目给出示例的误导,这里存在一个误区:在不选父节点时,子树选最优解不受任何干扰,也就是说子树的跟节点可选也可以不选。所以,可以将这个问题简化为,分选根节点和不选根节点,两种情况考虑:

  1. 选父节点,则子树必定不能选根节点;

  2. 如果不选跟节点,则子树可以在选根节点和不选跟节点中选最优解就好。

然后在这上面两种情况中选出最优解。

思考题

尝试使用动态规划来解决这个问题。

参考资料

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:

Input: [3,2,3,null,3,null,1]

     3
    / \
   2   3
    \   \
     3   1

Output: 7
Explanation: Maximum amount of money the thief can rob = <font color="red" style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">3 + <font color="red" style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">3  + <font color="red" style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">1  = <b style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">7* .

Example 2:

Input: [3,4,5,1,3,null,1]

     3
    / \
   4   5
  / \   \
 1   3   1

Output: 9
Explanation: Maximum amount of money the thief can rob = 4 + <font color="red">5 = 9.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * Runtime: 0 ms, faster than 100.00% of Java online submissions for House Robber III.
 * Memory Usage: 41.3 MB, less than 13.89% of Java online submissions for House Robber III.
 *
 * Copy from: https://leetcode-cn.com/problems/house-robber-iii/solution/java-2ms-by-horanol/[java 2ms - 打家劫舍 III - 力扣(LeetCode)]
 */
public int rob(TreeNode root) {
    int[] robs = doRob(root);
    return Math.max(robs[0], robs[1]);
}

public int[] doRob(TreeNode root) {
    int[] result = new int[2];
    if (Objects.isNull(root)) {
        return result;
    }
    int[] left = doRob(root.left);
    int[] right = doRob(root.right);
    result[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
    result[1] = left[0] + right[0] + root.val;
    return result;
}