这道题我的思路是错的,没有写出来,看了别人的思路
用递归来写代码,但是效率不高
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0;
public int rob(TreeNode root) {
if(root == null){
return 0;
}
int left = rob(root.left);
int right = rob(root.right);
int leftleft = root.left == null? 0:rob(root.left.left)+rob(root.left.right);
int rightright = root.right == null? 0:rob(root.right.left)+rob(root.right.right);
return Math.max(left+right,leftleft+rightright+root.val);
}
}