?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.

Why this solution does not work?

/**
 * 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) {
        List<Integer> res = new LinkedList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if(root==null) return 0;
        queue.add(root);
        while(!queue.isEmpty())
        {
            int levelnum = queue.size(); //这层有几个TreeNode
            int levelval = 0;
            for(int i=0; i<levelnum;i++)
            {
                TreeNode node = queue.poll();
                if(node.left!=null) queue.add(node.left);
                if(node.right!=null) queue.add(node.right);
                levelval = levelval + node.val;
            }
            res.add(levelval);
        }

        if(res.size()<=1)
            return res.size()==0?0:res.get(0);
        int[] dp = new int[res.size()];
        //init
        dp[0] = res.get(0);
        //the second is should to be calculate
        dp[1] = res.get(0)>res.get(1)?res.get(0):res.get(1);

        for(int i=2;i<res.size();i++)
            dp[i] = Math.max(dp[i-1], dp[i-2]+res.get(i));
        return dp[res.size() -1 ];
    }
}
时间: 2024-12-20 00:23:12

?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

&amp;lt;LeetCode OJ&amp;gt; 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

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

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