Skip to content

Commit 908fd6c

Browse files
committed
auto commit
1 parent 156f7a6 commit 908fd6c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

notes/Leetcode 题解 - 树.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,18 @@ Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
427427
```
428428

429429
```java
430+
Map<TreeNode, Integer> cache = new HashMap<>();
431+
430432
public int rob(TreeNode root) {
431433
if (root == null) return 0;
434+
if (cache.containsKey(root)) return cache.get(root);
432435
int val1 = root.val;
433436
if (root.left != null) val1 += rob(root.left.left) + rob(root.left.right);
434437
if (root.right != null) val1 += rob(root.right.left) + rob(root.right.right);
435438
int val2 = rob(root.left) + rob(root.right);
436-
return Math.max(val1, val2);
439+
int res = Math.max(val1, val2);
440+
cache.put(root, res);
441+
return res;
437442
}
438443
```
439444

0 commit comments

Comments
 (0)