<LeetCode OJ> 337. House Robber III

原创 2016年03月14日 23:41:39

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. 

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.

分析:

下面的答案有错,不知道错在哪里!!!难道不是统计偶数层的和与奇数层的和,然后比较大小就可得出结果?

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int rob(TreeNode* root) {
        if(root==NULL)  
            return 0;  
        queue<TreeNode*> que;//用来总是保存当层的节点  
        que.push(root);  
        int oddsum =root->val;//用于统计奇数层的和
        int evensum=0;  //用于统偶数层的和
        //获取每一层的节点
        int curlevel=2;
        while(!que.empty())  
        {  
            int levelSize = que.size();//通过size来判断当层的结束  
            for(int i=0; i<levelSize; i++)   
            {  
                if(que.front()->left != NULL) //先获取该节点下一层的左右子,再获取该节点的元素,因为一旦压入必弹出,所以先处理左右子  
                    que.push(que.front()->left);  
                if(que.front()->right != NULL)   
                    que.push(que.front()->right);  
                if(curlevel % 2 ==1)
                    oddsum  += que.front()->val;
                else
                    evensum += que.front()->val;
                que.pop();  
            }  
            curlevel++;
        }
        return oddsum > evensum ? oddsum : evensum;//奇数层的和与偶数层的和谁更大谁就是结果
    }
};


学习别人的代码:

int rob(TreeNode* root) {
    int child = 0, childchild = 0;
    rob(root, child, childchild);
    return max(child, childchild);
}

void rob(TreeNode* root, int &child, int &childchild) {
    if(!root) return;

    int l1 = 0, l2 = 0, r1 = 0, r2 = 0;
    rob(root->left, l1, l2);
    rob(root->right, r1, r2);

    child = l2 + r2 + root->val;
    childchild = max(l1, l2) + max(r1, r2);
}




注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50890931

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

版权声明:本文为EbowTang原创文章,后续可能继续更新本文。如果转载,请务必复制本文末尾的信息!

LeetCode_OJ【337】 House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this a...
  • hb_peng
  • hb_peng
  • 2016年09月02日 10:25
  • 103

LeetCode #337 - House Robber III - Medium

ProblemThe thief has found himself a new place for his thievery again. There is only one entrance to...
  • Arcome
  • Arcome
  • 2016年11月09日 18:54
  • 155

[leetcode]337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this a...

leetcode_337. House Robber III 大盗抢劫,房子是二叉树形式,深度优先遍历

题目: The thief has found himself a new place for his thievery again. There is only one entrance ...

LeetCode 337. House Robber III

大概题意:给定一棵二叉树,每个节点都有一个值,要求从其中选取若干个节点,是的值的和最大,约束条件为任意两个选取的节点之间不能相连。           解题的思路也很清晰,我们可以用动态规划的做法,...
  • isNoel
  • isNoel
  • 2016年12月22日 18:49
  • 165

leetcode 337:House Robber III

leetcode 337:House Robber III。leetcode新题,递归的动态规划

leetcode_middle_31_337. House Robber III

分析: 找出二叉树中最大的不相邻结点的值的和 题意: 一开始的想法是用一个布尔型参数确定下一个结点的值取不取,想的是就两种情况,一层取,下一层就不去...: private void h...
  • pusude
  • pusude
  • 2017年02月08日 19:39
  • 107

LeetCode - 337. House Robber III

这道题目具有Binary Tree的结构并且拥有最优子结构这一性质,也就是说如果我们想从当前的root抢劫到最多的钱,那么我们也希望在root的left和right子树上面抢到最多的钱。题目中已经给出...

[Leetcode] 337. House Robber III 解题报告

题目: The thief has found himself a new place for his thievery again. There is only one entrance ...

LeetCode337. House Robber III

题目: https://leetcode.com/problems/house-robber-iii/ The thief has found himself a new place for hi...
  • codeTZ
  • codeTZ
  • 2016年03月23日 20:34
  • 1015
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:<LeetCode OJ> 337. House Robber III
举报原因:
原因补充:

(最多只允许输入30个字)