Fix issue 1074
This commit is contained in:
parent
156f7a67f4
commit
24c6cccbc0
|
@ -428,12 +428,17 @@ Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int rob(TreeNode root) {
|
public int rob(TreeNode root) {
|
||||||
if (root == null) return 0;
|
int[] num = dfs(root);
|
||||||
int val1 = root.val;
|
return Math.max(num[0], num[1]);
|
||||||
if (root.left != null) val1 += rob(root.left.left) + rob(root.left.right);
|
}
|
||||||
if (root.right != null) val1 += rob(root.right.left) + rob(root.right.right);
|
private int[] dfs(TreeNode node) {
|
||||||
int val2 = rob(root.left) + rob(root.right);
|
if (node == null) return new int[2];
|
||||||
return Math.max(val1, val2);
|
int[] left = dfs(node.left);
|
||||||
|
int[] right = dfs(node.right);
|
||||||
|
int[] res = new int[2];
|
||||||
|
res[0] = left[1] + right[1] + node.val;
|
||||||
|
res[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user