leetcode337

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode337相关的知识,希望对你有一定的参考价值。

/**
 * 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的主要内容,如果未能解决你的问题,请参考以下文章

leetcode337

[leetcode-337-House Robber III]

[leetcode] 337.House Robber III

动态规划_leetcode337

<LeetCode OJ> 337. House Robber III

Leetcode 337. 打家劫舍 III