auto commit
This commit is contained in:
parent
156f7a67f4
commit
908fd6c8e0
|
@ -427,13 +427,18 @@ Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
|
|||
```
|
||||
|
||||
```java
|
||||
Map<TreeNode, Integer> cache = new HashMap<>();
|
||||
|
||||
public int rob(TreeNode root) {
|
||||
if (root == null) return 0;
|
||||
if (cache.containsKey(root)) return cache.get(root);
|
||||
int val1 = root.val;
|
||||
if (root.left != null) val1 += rob(root.left.left) + rob(root.left.right);
|
||||
if (root.right != null) val1 += rob(root.right.left) + rob(root.right.right);
|
||||
int val2 = rob(root.left) + rob(root.right);
|
||||
return Math.max(val1, val2);
|
||||
int res = Math.max(val1, val2);
|
||||
cache.put(root, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user