<LeetCode OJ> 337. House Robber III

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

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

转载于:https://www.cnblogs.com/yutingliuyl/p/7225828.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108303.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何下载pycharm以前的版本_pycharm版本区别

    如何下载pycharm以前的版本_pycharm版本区别地址:https://www.jetbrains.com/pycharm/download/previous.html点击进去就能看到各个版本的下载链接。

    2022年8月25日
    5
  • 我的世界区块显示_我的世界怎么显示区块线

    我的世界区块显示_我的世界怎么显示区块线我的世界手游区块是一个独特的机制,很多玩家对于区块是什么不太了解,区块显示指令以及区块的产生不是很熟悉,为了帮助到大家,今天小编就为大家带来我的世界手游区块显示指令分享:区块玩法操作详解的内容,希望大家能够喜欢,下面就让我们一起来看看吧!区块相关1.出生点区块在出生点附近的区块是一块围绕世界出生点的区域中的一个区块,只要有玩家在主世界,它就不会被从内存中卸载。这意味着像红石元件和刷怪会继续,甚至所…

    2022年9月17日
    2
  • 以太网RJ45 接线标准 线序(备忘)「建议收藏」

    以太网RJ45 接线标准 线序(备忘)「建议收藏」RJ是RegisteredJack的缩写,意思是“注册的插座”。在FCC(美国联邦通信委员会标准和规章)中的定义是,RJ是描述公用电信网络的接口,常用的有RJ-11和RJ-45,计算机网络的RJ-45是标准8位模块化接口的俗称。568A的排线顺序从左到右依次为:白绿、绿、白橙、蓝、白蓝、橙、白棕、棕。568B的排线顺序从左到右依次为:白橙、橙、白绿、蓝、白蓝、绿、白棕、棕。所谓的交叉线是指:一端…

    2022年9月15日
    2
  • 统一用户登录管理认证LDAP 服务端部署

    网上找了好多关于LDAP统一账户管理的文件,好多都是粘贴复制,能用得上的少之又少,正好最近又用到这个,于是着手看了郭老师的视频,顺便把自己学习的过程记录下来,供大家学习参考。1、实验环境:[root@localhost~]#cat/etc/redhat-releaseCentOSLinuxrelease7.2.1511(Core)[root@loca…

    2022年4月6日
    56
  • SpringCloud(二)—-Ribbon简介

    SpringCloud(二)—-Ribbon简介

    2020年11月12日
    208
  • AIC准则的理解

    AIC准则的理解本文介绍AIC准则的产生及应用。

    2022年5月10日
    83

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号