Skip to content

Commit 3b0e0a8

Browse files
authored
Merge pull request #2532 from Anukriti167/patch-9
Create 0337-house-robber-iii.java
2 parents 423062b + 2cdea0c commit 3b0e0a8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

java/0337-house-robber-iii.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int[] dfs(TreeNode root){
3+
if(root == null) return new int[2];
4+
5+
int []left = dfs(root.left);
6+
int []right = dfs(root.right);
7+
8+
int []res = new int[2];
9+
10+
res[0] = left[1] + right[1] + root.val; //with Root
11+
res[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); //without Root
12+
13+
return res;
14+
}
15+
public int rob(TreeNode root) {
16+
int []ans = dfs(root);
17+
return Math.max(ans[0], ans[1]);
18+
}
19+
}

0 commit comments

Comments
 (0)