• 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.
    
     

    Approach #1: C++. [native]

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int rob(TreeNode* root) {
            if (root == NULL) return 0;
            
            int val = 0;
            
            if (root->left != NULL) 
                val += rob(root->left->left) + rob(root->left->right);
    
            if (root->right != NULL) 
                val += rob(root->right->left) + rob(root->right->right);
    
            return max(val+root->val, rob(root->left)+rob(root->right));
        }
    };
    

      

    Approach #2: Java. [native + memory]

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int rob(TreeNode root) {
            if (root == null) return 0;
            
            return helper(root, new HashMap<>());
        }
        
        private int helper(TreeNode root, Map<TreeNode, Integer> map) {
            if (root == null) return 0;
            if (map.containsKey(root)) return map.get(root);
            int val = 0;
            if (root.left != null) 
                val += helper(root.left.left, map) + helper(root.left.right, map);
            if (root.right != null)
                val += helper(root.right.left, map) + helper(root.right.right, map);
            val = Math.max(val + root.val, helper(root.left, map) + helper(root.right, map));
            map.put(root, val);
            
            return val;
        }
    }
    

      

    Approach #3: Python. [dfs + DP]

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def rob(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            def superRob(root):
                if root == None:
                    return (0, 0)
                
                left, right = superRob(root.left), superRob(root.right)
                
                now = left[1] + right[1] + root.val
                
                later = max(left) + max(right)
                
                return (now, later)
            
            return max(superRob(root))
    

      

    Analysis:

    Maybe this is very hard to understand. I aways fall into the endless recursive. In those similar question we shouldn't try to image the process using our brain. 

    what we can do is to think the problem if or not can resolve the big problem into a little scope with the same condition . if so we should find the end condition 

    to stop the recursive and return the value(ie. 0 or someting other).

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    DL/T 467-2019 电站磨煤机及制粉系统性能试验
    fidlder-05(拦截并修改数据)
    fiddler-04(怎么对APP抓包)
    Redis5设计与源码分析读后感(二)简单动态字符串SDS
    Jedis连接搭建在阿里云服务器上的Redis,基于Linux(CentOS7)
    centos7下安装redis6.0版本+3种启动方式
    Linux下端口被占用的解决方法
    Linux卸载Nginx
    linux中普通用户修改密码出现(passwd:Authentication token manipulation error)
    linux重置密码提示与用户名相似该怎么解决?
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10105045.html
Copyright © 2020-2023  润新知