forked from jyxia/LeetCode-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path337-houseRobberIII.js
65 lines (56 loc) · 2.05 KB
/
337-houseRobberIII.js
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
64
65
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* Key: similar to every house in previous hourse rob problems, each node store two values,
* first value is to store the max value if the node will be robbed. The second value is the vaule
* if the node will not be robbed.
* In the first case, the value is straightforward to calculate, that is, current node's val
* plus the second value from it's child nodes' second value (not robbed).
* In the second case, because the node is not going to be robbed, its children can be robbed
* or not robbed. So the value is the max value of the left child's two values plus the right child's
* max value from the two values.
*
* @param {TreeNode} root
* @return {number}
*/
var rob = function(root) {
var result = robDFS(root);
return Math.max(result[0], result[1]);
};
var robDFS = function(root) {
if (!root) return [0, 0];
var result = [];
var leftResults = robDFS(root.left);
var rightResults = robDFS(root.right);
result[0] = root.val + leftResults[1] + rightResults[1];
result[1] = Math.max(leftResults[0], leftResults[1]) + Math.max(rightResults[0], rightResults[1]);
return result;
};
// not right, why?
var rob = function(root) {
var cachedResult = {};
return robHelper(root, cachedResult);
};
var robHelper = function(root, cachedResult) {
if (!root) return 0;
if (cachedResult.hasOwnProperty(root)) {
return cachedResult[root];
}
var val = 0;
if (root.left !== null) {
val += robHelper(root.left.left, cachedResult) + robHelper(root.left.right, cachedResult);
console.log(root, val);
}
if (root.right !== null) {
val += robHelper(root.right.left, cachedResult) + robHelper(root.right.right, cachedResult);
console.log(root, val);
}
val = Math.max(val + root.val, robHelper(root.left, cachedResult) + robHelper(root.right, cachedResult));
cachedResult[root] = val;
return val;
};