337_House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

     3
    /    2   3
    \   \
     3   1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

     3
    /    4   5
  / \   \
 1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

一个二叉树,不能取相邻的两个数,求可以取到的树之和的最大值

rob[2]中 rob[0]代表rob节点所能得到的最大值,rob[1]代表从rob的子节点到叶子节点所能得到的最大值。用类似深度优先的遍历,动态规划得出结果

/**
 * 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) {
        return dfs(root)[0];
    }

    public int[] dfs(TreeNode root)
    {
        int[] rob = {0, 0};
        if(root != null)
        {
            int[] leftRob = dfs(root.left);
            int[] rightRob = dfs(root.right);
            rob[1] = leftRob[0] + rightRob[0];
            rob[0] =  System.Math.Max(rob[1], leftRob[1] + rightRob[1] + root.val);
        }
        return rob;
    }
}
时间: 2024-11-05 14:45:26

337_House Robber III的相关文章

LeetCode House Robber III

原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent hous

Leetcode题目:House Robber III

题目: The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all

leetcode笔记:House Robber III

一. 题目描述 The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "

337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou

[leetcode] 337.House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou

337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou

<LeetCode OJ> 337. House Robber III

Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent

?House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou

337. House Robber III java solutions

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou