-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfourth.java
63 lines (54 loc) · 2.04 KB
/
fourth.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
59
60
61
62
63
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class fourth {
public int rob(TreeNode root) {
if(root==null){
return 0;
}
TreeNode not=new TreeNode(0);
TreeNode taken=new TreeNode(0);
rob(root,not,taken);
return Math.max(not.val,taken.val);
}
public void rob(TreeNode rob,TreeNode not,TreeNode taken){
if(rob.right==null && rob.left==null){
taken.val=rob.val;
not.val=0;
}else if(rob.left==null){
not.right=new TreeNode(0);
taken.right=new TreeNode(0);
rob(rob.right,not.right,taken.right);
taken.val=rob.val+not.right.val;
not.val=Math.max(taken.right.val,not.right.val);
}else if(rob.right==null){
not.left=new TreeNode(0);
taken.left=new TreeNode(0);
rob(rob.left,not.left,taken.left);
taken.val=rob.val+not.left.val;
not.val=Math.max(taken.left.val,not.left.val);
}else{
not.right=new TreeNode(0);
not.left=new TreeNode(0);
taken.right=new TreeNode(0);
taken.left=new TreeNode(0);
rob(rob.right,not.right,taken.right);
rob(rob.left,not.left,taken.left);
taken.val=rob.val+not.left.val+not.right.val;
not.val=Math.max(taken.left.val,not.left.val)
+Math.max(taken.right.val,not.right.val);
}
}
}
/* ideal: did not seam useful here
d7: what i saw was whats that we need to remember at every point what happens from the 2 choices before.
so the question is do i rob or do i not is not done until you have every point in the tree stored some how
so i make 2 trees to follow the first tree and i build them from bottem to top, one storeing what happens if i take
and the second what if i dont.
*/