# 打劫房屋 III

  3
/ \
2   3
\   \
3   1


    3
/ \
4   5
/ \   \
1   3   1


/**
* Definition of TreeNode:
* public class TreeNode {
*     public int val;
*     public TreeNode left, right;
*     public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: The maximum amount of money you can rob tonight
*/
Map<TreeNode, Integer> M = new HashMap<TreeNode, Integer>();
public int houseRobber3(TreeNode root) {
// write your code here
if(root==null)
return 0;
if(M.containsKey(root))
return M.get(root);
/*加了这段就有错。。。莫非这中间还能有负数。。。不懂
if(root.left==null&&root.right==null)
return root.val;
*/
//rob
int rob = root.val;
if(root.left!=null){
rob += houseRobber3(root.left.left);
rob += houseRobber3(root.left.right);
}
if(root.right!=null){
rob += houseRobber3(root.right.left);
rob += houseRobber3(root.right.right);
}

int noRob = houseRobber3(root.left) + houseRobber3(root.right);
M.put(root, Math.max(rob, noRob));
return Math.max(rob, noRob);
}
}

05-30 33

04-24 33

05-05 43

05-30 71

03-24 51

02-24 1万+

09-02 138

03-25 142

03-24 39

01-04 25

#### [Leetcode] 第337题 打家劫舍III

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。