```
public class Solution {
public static int rob(TreeNode root) {
Solution result = solve(root);
return Math.max(result.with, result.without);
}
private static Solution solve(TreeNode root) {
if (root == null) return new Solution();
Solution left = solve(root.left);
Solution right = solve(root.right);
Solution result = new Solution();
result.with = Math.max(root.val + left.without + right.without, left.with + right.with);
result.without = left.with + right.with;
return result;
}
private int with = 0;
private int without = 0;
};
```