Merge 24c6cccbc0ba401d0d7e9515766cc275d13f2e4f into 156f7a67f4bd7fc1c59ab82473b6d859d69c667a

This commit is contained in:
daidai21 2021-04-14 02:43:23 +08:00 committed by GitHub
commit da26b43735
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -428,12 +428,17 @@ Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
```java
public int rob(TreeNode root) {
if (root == null) return 0;
int val1 = root.val;
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);
int val2 = rob(root.left) + rob(root.right);
return Math.max(val1, val2);
int[] num = dfs(root);
return Math.max(num[0], num[1]);
}
private int[] dfs(TreeNode node) {
if (node == null) return new int[2];
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;
}
```