zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    laravel 5.5 仓库模式 文件之间接口与实现操作
    php 无线分类 根据子级找父级
    php 无限极分类,根据父级 找子级
    laravel5.4 中 dd和dump的区别。
    laravel hash密码生成和密码验证
    oracle建表详细信息
    关于组件的认识
    java的Thread Dump诊断工具
    weblogic连接池
    详解Oracle数据字典
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10105045.html
Copyright © 2011-2022 走看看