Leetcode题目: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:

     3
    /    2   3
    \   \
     3   1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

     3
    /    4   5
  / \   \
 1   3   1

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

题目解答:

这个题目是在之前House Robber 和House Robber II之上的变体。它要求的是在树上的动归。需要用到深度优先搜索和动归的思想。一样的,相邻节点不能偷。

另外借助一个辅助结构,来帮助我们存动归中需要比较的两个值。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

struct robMoney  //辅助数据结构,用来记录动态规划过程中的两个整型值
{
   int noRob;   //不偷当前这一家,可以得到的金钱数目
   int curRob;  //在当前这个位置,可以获得到的金钱数目最大值
   robMoney():noRob(0),curRob(0){}
};

class Solution {
public:
    int rob(TreeNode* root) {
        if(root == NULL)
            return 0;
        robMoney res = dfs(root);
        return res.curRob;//在当前位置所能获得的金钱数目最大值
    }

    robMoney dfs(TreeNode *root)  //深度优先搜索
    {
        robMoney money;
        if(root == NULL)
        {
            return money;
        }
        robMoney left = dfs(root -> left);
        robMoney right = dfs(root -> right);
        money.noRob = left.curRob + right.curRob; //当前这家选择不偷
        money.curRob = max(money.noRob, left.noRob + right.noRob + root -> val); //取二者的最大值
        return money;
    }

    int max(int a, int b)
    {
        return a > b ? a : b;
    }
};

  

时间: 2024-08-08 01:29:31

Leetcode题目:House Robber III的相关文章

LeetCode题目:House Robber

House Robber: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and

[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

【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 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->

一道简单的动态规划题目——House Robber

一.题目 House Robber(一道Leetcode上的关于动态规划的简单题目)具体描述如下: There is a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent hous

leetcode题目:Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

leetcode题目思路以及部分解答(一)

为了进好公司这一个多月就得抽时间刷leetcode了..感觉这个OJ很不严谨...好多边界条件都没说清处..不过还好可以推测.唯一的好处就是不用自己编译调试,可以直接在网上显示出结果.当然,复杂一点的题目为了调试自己构建题目的结构也是很麻烦的...所以我发现提交里面错误好多.....再就是在笔记本上会时不时的变卡...每次提交都得等个3,4分钟才成功.要不就502错误... 我的题目按照通过率来.从通过率最高的题目开始讲解.每题不一定是最优解,都是我想得.仅供参考. 题目标题我都标好了.可以用c

leetcode题目思路以及部分解答(二)

又刷了30题了,这速度还不错.因为还有别的东西要复习,所以进度并不快.感觉还是能学到很多新东西的.早知道这个就不用去其他地方刷了.这个难度不高,还可以知道哪些情况没考虑.比其他OJ那种封闭式的好多了.还是进入正题吧. 1.Rotate Image 这个做过两三次了,但每次还是得重新开始推导..这次又推导了很久..不过好在做过,代码也写得比较简洁. 主要思路就是第一层循环按层次深入.第二层把旋转后对应替代的4个位置循环更新.swap就是用来更新用的.做完发现讨论里的最高票代码就是我这样子= =  

leetcode题目:Palindrome Partitioning 和Palindrome Partitioning II

题目一: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",

leetcode题目:Sum Root to Leaf Numbers和Longest Consecutive Sequence

题目一: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 /