您的位置:首页 > 其它

House Robber III

2016-07-24 02:35 337 查看
/**
* 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) {
if (root == null) {
return 0;
}
int[] res = helper(root);
return Math.max(res[0], res[1]);
}

private int[] helper(TreeNode node) {
if (node == null) {
return new int[]{0, 0};
}
int[] left = helper(node.left);
int[] right = helper(node.right);
int[] res = new int[2];
res[0] = node.val + left[1] + right[1];
res[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: