Two solutions with Java (1 ms)
public int rob(TreeNode root) {\n        if(root==null) return 0;\n        rob1(root);\n        return root.val;\n    }\n    public int rob1(TreeNode root){\n  
    public int rob(TreeNode root) {\n        if(root==null) return 0;\n        rob1(root);\n        return root.val;\n    }\n    public int rob1(TreeNode root){\n        if(root==null) return 0;\n        int pre=0;\n        root.val+=rob1(root.left)+rob1(root.right);\n        if(root.left!=null)  pre+=root.left.val;\n        if(root.right!=null)  pre+=root.right.val;\n        root.val=Math.max(pre,root.val);\n        return pre;\n    }\nThe other solution need extra class:\n\n    class Profit{\n       int preprofit;\n       int maxprofit;\n       Profit(int pre,int max){\n           preprofit=pre;\n           maxprofit=max;\n       }\n    }\n    public class Solution {\n         public int rob(TreeNode root ) {\n              return rob1(root).maxprofit;\n         }\n         public Profit rob1(TreeNode root){\n               if (root == null) return new Profit(0, 0);\n               int pre = 0, max = root.val;\n               Profit left = rob1(root.left), right = rob1(root.right);\n               max += left.preprofit + right.preprofit;\n               pre = left.maxprofit + right.maxprofit;\n               max = Math.max(pre,max);\n               return new Profit(pre,max);\n         }\n    }
[]