Recursive Java Solution, easy to understand


  • 0
    public class Solution {
        public int rob(TreeNode root) {
            return helper(false, root);
        }
        public int helper(boolean lastState, TreeNode cur){
            if(cur == null)
                return 0;
            if(lastState == false){
                int m = cur.val + helper(true, cur.left) + helper(true, cur.right);
                int n = helper(false, cur.left) + helper(false, cur.right);
                return m > n ? m : n;
            }
            else return helper(false, cur.left) + helper(false, cur.right);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.