Skip to content

Commit

Permalink
Create 0337-house-robber-iii.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Anukriti167 authored May 31, 2023
1 parent f02c591 commit 2cdea0c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions java/0337-house-robber-iii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int[] dfs(TreeNode root){
if(root == null) return new int[2];

int []left = dfs(root.left);
int []right = dfs(root.right);

int []res = new int[2];

res[0] = left[1] + right[1] + root.val; //with Root
res[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); //without Root

return res;
}
public int rob(TreeNode root) {
int []ans = dfs(root);
return Math.max(ans[0], ans[1]);
}
}

0 comments on commit 2cdea0c

Please sign in to comment.