Rikko-337 Robbery III (dp)

Rikko-337 House Robbery III

1. Topic

337. House Robbery III

Thieves have discovered a new area to steal from. There is only one entrance to this area , let's call it root .

In addition to the root, each house has one and only one "parent" house connected to it. After some reconnaissance, the clever thief realized that "all the houses in this place are arranged like a binary tree ". If two directly connected houses are robbed on the same night, the houses will automatically call the police.

Root of the given binary tree. Returns the maximum amount a thief can steal without triggering the alarm .

Example 1:

img

输入: root = [3,2,3,null,3,null,1]
输出: 7 
解释: 小偷一晚能够盗取的最高金额 3 + 3 + 1 = 7

2. Analysis

  1. topic. Obviously, this is a binary tree , so we can think of whether it is front, middle, back, depth traversal, or layer order traversal. To simplify the topic, if there are only three nodes, left and right of the root, there are two situations here, one is to have the root node, and the other is to only need the left and right child nodes. That is, the root node is taken out separately, and it is obvious that we use post-order traversal.
  2. The first is direct recursion to solve, but it will time out. Here we can use a map to store and calculate the value after a certain node to avoid some repeated calculations. The second is to use the technique dp[2], dp[0] means not to select the node, and dp[1] means to select the node.
  3. Because the left and right need to be separated from the root node , we compare the value of whether the root node is needed, and the larger one is what we need.

3. Code and Analysis

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    
    public int rob(TreeNode root) {
    
    
        Map<TreeNode, Integer> map = new HashMap<>();
        int[] res = rob2(root);
        return Math.max(res[0], res[1]);
    }
    // 1. 第一种方式 递归 + 存值
    public int rob1(TreeNode root, Map<TreeNode, Integer> map){
    
    
        if (root == null) return 0;

        if (map.containsKey(root)){
    
    
            return map.get(root);
        }

        int money = root.val;
        
        if (root.left != null){
    
    
            money += rob1(root.left.left, map) + rob1(root.left.right, map);
        }
        if (root.right != null){
    
    
            money += rob1(root.right.left, map) + rob1(root.right.right, map);
        }
        map.put(root, Math.max(money, rob1(root.left, map) + rob1(root.right, map)));

        return Math.max(money, rob1(root.left, map) + rob1(root.right, map));
    }
    
    // 2.第二种方式 dp[2]
    public int[] rob2(TreeNode root){
    
    
        int[] res = new int[2];
        if (root == null) return res;

        int[] left = rob2(root.left);
        int[] right = rob2(root.right);

        res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        res[1] = root.val + left[0] + right[0];

        return res;
    }

}

4. Practice

Link to link to the topic : 337. Robbery III

Guess you like

Origin blog.csdn.net/qq_51326491/article/details/129332530