<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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • BigDecimal 与 int,long,double之间的互转[通俗易懂]

    BigDecimal 与 int,long,double之间的互转[通俗易懂]BigDecimal与int,long,double之间的互转转换关系如下:int转换成BigDecimal/***int转Bigdecimal*/@Testpublicvoiddemo04(){inta=101;BigDecimalbig=newBigDecimal(a);System.out.prin…

    2022年5月26日
    63
  • JS中的位置和宽度:clientWidth、offsetWidth、scrollWidth等区别「建议收藏」

    JS中的位置和宽度:clientWidth、offsetWidth、scrollWidth等区别「建议收藏」首先定义一个div。然后稍微装修一下下面开始区分一、clientWidth和clientHeigh、clientTop和clientLeft1,clientWidth的实际宽度clientWidth=width+左右padding2,clientHeigh的实际高度clien…

    2022年7月22日
    14
  • Git 常用命令

    Git 常用命令

    2021年9月17日
    51
  • Discuz二次开发相应知识点

    Discuz二次开发相应知识点“Discuz”在下文中简称“DZ”。要弄DZ二次开发,必须至少具备如下技能:1)能够理很好理解MVC构架的原理(虽然DZ不是MVC架构的)2)扎实的PHP基础,熟悉结构化程序,OOP程序的写法及应用3)熟悉MYSQL就用,掌握SQL语言,懂SQL优化者更佳4)熟悉使用Discuz的各项功能一)Discuz的文件系统目录注:想搞DZ开发,就得弄懂DZ中每个文件的功能。

    2022年5月19日
    39
  • Python读txt(python打开txt文件)

    python读写txt文件准备原始txt数据3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110…

    2022年4月17日
    68
  • 如何高效学习PLC

    如何高效学习PLC【1】电工原理和电机原理一定要懂,简单的就记背也要背下来,比如马达容量1KW2A,正反转,星三角接线,电线容量。电阻,电感,电容的特性等;【2】液压和气动也要掌握,比如压力换算,压力和电流的比例换算,这在有压力控制上都要用到;【3】电线截面要会看,线拿到手就知道几平方的,还有什么电器上该用什么线,比如马达就用4线的,3根主线1根接地。从变频器上出来的要用屏蔽线;【4】机修也要会做,特别是螺丝…

    2022年10月19日
    2

发表回复

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

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