Simple Java Solution


  • 0
    D

    ...
    public class Solution {
    public int rob(TreeNode root) {
    int[] ret = helper(root);
    return ret[1];
    }

    int[] helper(TreeNode node) {
        //ret[0] is previous nodes max sum value, ret[1] is current node max sum value
        int[] ret = new int[2];
        if (node == null) {
            ret[0] = 0;
            ret[1] = 0;
            return ret;
        }
        int[] left = helper(node.left);
        int[] right = helper(node.right);
        ret[0] = left[1] + right[1];
        ret[1] = Math.max(ret[0], node.val + left[0] + right[0]);
        return ret;
    }
    

    }
    ...


Log in to reply
 

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