码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode337

时间:2017-06-09 10:05:43      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:problems   int   rip   .com   ber   node   http   root   ret   

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int Rob(TreeNode root)
        {
            int[] num = dfs(root);
            return Math.Max(num[0], num[1]);
        }

        private int[] dfs(TreeNode x)
        {
            if (x == null) return new int[2];
            int[] left = dfs(x.left);
            int[] right = dfs(x.right);
            int[] res = new int[2];
            res[0] = left[1] + right[1] + x.val;
            res[1] = Math.Max(left[0], left[1]) + Math.Max(right[0], right[1]);
            return res;
        }
}

https://leetcode.com/problems/house-robber-iii/#/description

leetcode337

标签:problems   int   rip   .com   ber   node   http   root   ret   

原文地址:http://www.cnblogs.com/asenyang/p/6970247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!