forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHouseRobberIII.java
55 lines (45 loc) · 1.54 KB
/
HouseRobberIII.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class HouseRobberIII {
Map<TreeNode, Integer> cache = new HashMap<>(); // O(n)
public int rob(TreeNode root) { // TC O(n), each node is visited once
if(cache.containsKey(root)){
return cache.get(root);
}
if(root == null){
return 0;
} else{
int leftleftGrandChild =0 ;
int rightleftGrandChild =0 ;
int leftrightGrandChild =0 ;
int rightrightGrandChild =0 ;
if(root.left !=null){
leftleftGrandChild = rob(root.left.left);
leftrightGrandChild = rob(root.left.right);
}
if(root.right !=null){
rightleftGrandChild = rob(root.right.left);
rightrightGrandChild = rob(root.right.right);
}
int includingRoot = root.val + leftleftGrandChild + rightleftGrandChild + leftrightGrandChild + rightrightGrandChild;
int leftChild = rob(root.left);
int rightChild = rob(root.right);
int excludingRoot = leftChild + rightChild;
cache.put(root, Math.max(includingRoot, excludingRoot));
return cache.get(root);
}
}
}