小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为 root 。
除了 root 之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果 两个直接相连的房子在同一天晚上被打劫 ,房屋将自动报警。
给定二叉树的 root 。返回 在不触动警报的情况下 ,小偷能够盗取的最高金额 。
JAVA:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int rob(TreeNode root) {
int[] i=dfs(root);
return Math.max(i[0],i[1]);//0代表偷,1代表不偷
}
public int[] dfs(TreeNode node){
//int[]{select的最优解,not_select最优解}
if(node==null){
return new int[]{0,0};
}
int[] l=dfs(node.left);
int[] r=dfs(node.right);
int select=node.val+l[1]+r[1];
int notSelect=Math.max(l[0],l[1])+Math.max(r[0],r[1]);
return new int[]{select,notSelect};
}
}