/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int rob(TreeNode root) {
int[] value = helper(root);
return Math.max(value[0], value[1]);
}
public int[] helper(TreeNode root) {
if (root == null) {
return new int[2];
}
int[] left = helper(root.left);
int[] right = helper(root.right);
int[] value = new int[2];
value[0] += Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
value[1] += root.val + left[0] + right[0];
return value;
}
}