LeetCode 337. House Robber III 动态演示

每个节点是个房间,数值代表钱。小偷偷里面的钱,不能偷连续的房间,至少要隔一个。问最多能偷多少钱

TreeNode* cur

mp[{cur, true}]表示以cur为根的树,最多能偷的钱

mp[{cur, false}]表示以cur为根的树,不偷cur节点的钱,最多能偷的钱

可以看出有下面的关系

mp[{node, false}] = mp[{node->left,true}] + mp[{node->right,true}]
mp[{node, true}] = max(node->val + mp[{node->left,false}] + mp[{node->right,false}], mp[{node, false}])

class Solution {
public:
    void helper(TreeNode* node, map<pair<TreeNode*, bool>,int>& mp){
        if(!node){
            return;
        }
        //a(node)
        //lk("root",node)

        helper(node->left, mp);
        helper(node->right, mp);
        mp[{node, false}] = mp[{node->left,true}] +  mp[{node->right,true}];
        mp[{node, true}] = max(node->val + mp[{node->left,false}] +  mp[{node->right,false}],
                               mp[{node, false}]);
        //dsp

    }

    int rob(TreeNode* root) {
        map<pair<TreeNode*, bool>,int> mp;
       //amap(mp, pair<TreeNode*, bool>,int)
        //ahd(root)
        mp[{NULL, true}]=0;
        mp[{NULL, false}]=0;
        helper(root, mp);
        return max(mp[{root, false}], mp[{root, true}]);
    }
};

动态演示 http://simpledsp.com/FS/Html/lc337.html

原文地址:https://www.cnblogs.com/leetcoder/p/11356529.html

时间: 2024-10-09 10:50:24

LeetCode 337. House Robber III 动态演示的相关文章

[leetcode] 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 hou

&amp;lt;LeetCode OJ&amp;gt; 337. House Robber III

Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium 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

&lt;LeetCode OJ&gt; 337. House Robber III

Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium 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

【LeetCode】House Robber III(337)

1. Description 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 tha

LeetCode in Python 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 hou

337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环

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 hou

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 hou

337. House Robber III java solutions

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 hou

LeetCode 114. Flatten Binary Tree to Linked List 动态演示

把二叉树先序遍历,变成一个链表,链表的next指针用right代替 用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素 class Solution { public: void helper(TreeNode* cur, TreeNode*& tail){ //a(tail) //lk("root",tail) //a(cur) //lk("root",cur) //dsp tail=cur; TreeNode* right=cur->