House Robber III LeetCode solution in Java

1
class Solution {
2
public int rob(TreeNode root) {
3
int ans[] = robHouse(root);
4
return Math.max(ans[0], ans[1]);
5
}
6

7
public int[] robHouse(TreeNode root) {
8
if (root == null) {
9
return new int[2];
10
}
11

12
int left[] = robHouse(root.left);
13
int right[] = robHouse(root.right);
14

15
int ans[] = new int[2];
16

17
ans[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
18
ans[1] = root.val + left[0] + right[0];
19

20
return ans;
21
}
22
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0