leetcode 337. House Robber III DP动态规划 + DFS深度有限遍历

旧城等待, 2022-06-07 01:13 233阅读 0赞

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.

这道题意很简单,就是父亲节点和自己不可以同时抢劫,这里设置一个返回数字res,其中res[0]表示抢劫当前节点, res[1]不抢劫当前节点,直接DFS递归遍历即可实现。

建议和这道题leetcode 740. Delete and Earn 动态规划DP、leetcode 198. House Robber 入室抢劫 + DP求解 和 这道题 leetcode 213. House Robber II 入室抢劫 抢劫问题 一起学习。

代码如下:

  1. /*class TreeNode {
  2. int val;
  3. TreeNode left;
  4. TreeNode right;
  5. TreeNode(int x) { val = x; }
  6. }*/
  7. /*
  8. * 这其实也是DP的解决方法之一,
  9. * */
  10. class Solution
  11. {
  12. public int rob(TreeNode root)
  13. {
  14. if(root==null)
  15. return 0;
  16. int []res=getRes(root);
  17. return Math.max(res[0], res[1]);
  18. }
  19. /*
  20. * res[0]表示抢劫当前节点的记录的最大值
  21. * res[1]表示不抢劫当前的结点的记录的最大值
  22. * */
  23. public int[] getRes(TreeNode root)
  24. {
  25. int[] res=new int[2];
  26. if(root==null)
  27. return res;
  28. else
  29. {
  30. int[] left=getRes(root.left);
  31. int[] right=getRes(root.right);
  32. //抢劫当前节点,然后不抢劫其子节点
  33. res[0]=root.val + left[1] + right[1];
  34. //不抢劫当前节点,然后可以抢劫或者不抢劫其子节点
  35. res[1]=Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
  36. return res;
  37. }
  38. }
  39. }

下面是C++的做法,和上面的思路一模一样

代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <unordered_map>
  5. #include <set>
  6. #include <unordered_set>
  7. #include <queue>
  8. #include <stack>
  9. #include <string>
  10. #include <climits>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <functional>
  14. #include <bitset>
  15. #include <numeric>
  16. #include <cmath>
  17. #include <regex>
  18. using namespace std;
  19. /*
  20. struct TreeNode
  21. {
  22. int val;
  23. TreeNode *left;
  24. TreeNode *right;
  25. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  26. };
  27. */
  28. class Solution
  29. {
  30. public:
  31. int rob(TreeNode* root)
  32. {
  33. vector<int> res = dfs(root);
  34. return max(res[0], res[1]);
  35. }
  36. vector<int> dfs(TreeNode* root)
  37. {
  38. vector<int> res(2, 0);
  39. if (root == NULL)
  40. return res;
  41. else
  42. {
  43. vector<int> left = dfs(root->left);
  44. vector<int> right = dfs(root->right);
  45. res[0] = root->val + left[1] + right[1];
  46. res[1] = max(left[0], left[1]) + max(right[0], right[1]);
  47. return res;
  48. }
  49. }
  50. };

发表评论

表情:
评论列表 (有 0 条评论,233人围观)

还没有评论,来说两句吧...

相关阅读