LeetCode 337. House Robber III

来源:互联网 发布:百度算法调整2017 编辑:程序博客网 时间:2024/05/17 05:53
/** * 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) {        return findMax(root)[0];    }        private int[] findMax(TreeNode root) {    int[] max = new int[2];    if (root == null) return max;    int[] l = findMax(root.left);    int[] r = findMax(root.right);    max[1] = l[0] + r[0];    max[0] = Math.max(max[1], l[1] + r[1] + root.val);    return max;    }}

0 0
原创粉丝点击