forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_337.java
58 lines (49 loc) · 1.71 KB
/
_337.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.HashMap;
import java.util.Map;
public class _337 {
public static class Solution1 {
//simple recursion without cacheing: 1189 ms
public int rob(TreeNode root) {
if (root == null) {
return 0;
}
int val = 0;
if (root.left != null) {
val += rob(root.left.left) + rob(root.left.right);
}
if (root.right != null) {
val += rob(root.right.left) + rob(root.right.right);
}
val = Math.max(val + root.val, rob(root.left) + rob(root.right));
return val;
}
}
public static class Solution2 {
//same idea, but with cacheing via a hashmap: 8 ms
public int rob_dp(TreeNode root) {
Map<TreeNode, Integer> map = new HashMap<>();
return getMaxValue(root, map);
}
private int getMaxValue(TreeNode root, Map<TreeNode, Integer> map) {
if (root == null) {
return 0;
}
if (map.containsKey(root)) {
return map.get(root);
}
int val = 0;
if (root.left != null) {
val += getMaxValue(root.left.left, map) + getMaxValue(root.left.right, map);
}
if (root.right != null) {
val += getMaxValue(root.right.left, map) + getMaxValue(root.right.right, map);
}
int max = Math.max(root.val + val,
getMaxValue(root.left, map) + getMaxValue(root.right, map));
map.put(root, max);
return max;
}
}
}