스터디 문제6번 Tree 정답률 49.9%
https://linlaw0229.github.io/2018/06/27/337-House-Robber-III/
337. House Robber III | linlaw Techblog
Problem description: The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “al
linlaw0229.github.io
public class Solution {
public int rob(TreeNode root) {
int[] ret = robSub(root);
return Math.max(ret[0], ret[1]);
}
private int[] robSub(TreeNode root) {
if (root == null) return new int[2];
int[] left = robSub(root.left);
int[] right = robSub(root.right);
int[] ret = new int[2];
ret[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
ret[1] = root.val + left[0] + right[0];
return ret;
}
}
댓글