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


相关推荐

  • 圆周率1千亿位_圆周率十亿位

    圆周率1千亿位_圆周率十亿位展开全部3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852…

    2025年11月8日
    2
  • viewpager嵌套viewpager

    viewpager嵌套viewpagerviewpager嵌套viewpager要求:外层不可以滑动,内层可以滑动实现:重写外层的viewpager的2个方法即可publicclassNoScrollViewPagerextendsViewPager{publicNoScrollViewPager(Contextcontext){super(context);}public

    2022年7月22日
    10
  • ffmpeg安装教程win10_nginx菜鸟教程

    ffmpeg安装教程win10_nginx菜鸟教程简述作为一个计算机方面的小白,对ffmpeg其实没多少了解,只是因为在合并音频和视频要使用到ffmpeg这个工具,所以才下载下来,所以就是一个简单的安装教程。话不多说开始安装吧。下载百度网盘可能有兄弟访问github不是很给力,直接下载这个也是可以的链接:https://pan.baidu.com/s/1Z7VkOv-_PAub6OfDkyly4Q提取码:yj5e官网下载来到官网下载点击跳转来到下载主页点击这个进入github,找到资源下载即可下载这个也可以,我下载的时候出现了很

    2025年11月10日
    3
  • PEST分析顺丰服务需求_快递行业宏观环境PEST分析[通俗易懂]

    PEST分析顺丰服务需求_快递行业宏观环境PEST分析[通俗易懂]精品welcome宏观环境PEST分析PEST分析又称大环境分析,是研究宏观环境的有效工具。通过Pest分析法,公司能够剖析出自身所处的外部大环境究竟对自己的发展是有利还是有害,以及据此作出战略规划,趋利避害。其中每一个字母各代表一个因素,分别为:P(political—政治)、E(economic—经济)、S(social—社会)、T(technological—技术),接下来将通过pest分析…

    2022年6月11日
    45
  • c语言createthread函数用法,CreateThread函数「建议收藏」

    c语言createthread函数用法,CreateThread函数「建议收藏」当使用CreateProcess调用时,系统将创建一个进程和一个主线程。CreateThread将在主线程的基础上创建一个新线程,大致做例如以下步骤:1在内核对象中分配一个线程标识/句柄,可供管理,由CreateThread返回2把线程退出码置为STILL_ACTIVE。把线程挂起计数置13分配context结构4分配两页的物理存储以准备栈。保护页设置为PAGE_READWRITE。第2页设为PA…

    2022年7月11日
    23
  • 公众号:正确响应微信发送的Token验证「建议收藏」

    直接把下面代码复制到你要填写的url地址<?php//定义常量tokendefine(‘TOKEN’,’weixin’);//检查标签functioncheckSignature(){//先获取到这三个参数$signature=$_GET[‘signature’];$nonce=…

    2022年4月14日
    440

发表回复

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

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