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


相关推荐

  • ActiveMQ objecmessage 无法调用问题。

    ActiveMQ objecmessage 无法调用问题。

    2021年6月16日
    104
  • 什么是SOA?

    SOA的定义SOA(Service-OrientedArchitecture),中文全称:面向服务的架构。SOA是一种粗粒度、松耦合服务架构,服务之间通过简单、精确定义接口进行通讯,不涉及底层编程接口和通讯模型。SOA的基本特征可从企业外部访问  随时可用  粗粒度的服务接口分级 松散耦合 可重用的服务服务接口设计管理  标准化的服务接口  支持各种消息模式 精…

    2022年4月4日
    43
  • BT渗透「建议收藏」

    BT渗透「建议收藏」PHP交流群:294088839,Python交流群:652376983 whois域名/ip查看域名的详细信息。ping域名/ip测试本机到远端主机是否联通。dig域名/ip查看域名解析的详细信息。host-l域名dns服务器传输zone。扫描nmap:-sS半开扫描TCP和SYN扫描。-sT完全TCP连接扫描。-sUUDP扫描-PSs…

    2022年4月29日
    49
  • 【android】在eclipse中查看genymotion模拟器的sd卡文件夹

    【android】在eclipse中查看genymotion模拟器的sd卡文件夹

    2022年2月4日
    43
  • 大数据分析应用的机遇与挑战「建议收藏」

    大数据分析应用的机遇与挑战「建议收藏」随着信息技能的发展,互联网家当的进步,计算机数据处理能力的快速增长,电子商务的日新月异及各种社交媒体的传播扩散,各种信息无时无刻不在影响着我们的生活。我们每时每刻都在自觉或者不自觉得与数据打交道,成为数据的记录者与传播者。海量数据的处理,以及如何利用大数据营销,给我们提出了更多的挑战。在这个人人都高喊“大数据时代”的今天,数据似乎被提到一个前所未有的高度。无论是个人还是企业,无论是网络营销还是线…

    2022年5月15日
    37
  • onLoad和onShow的使用

    onLoad和onShow的使用onLoad页面第一次加载时执行,只执行一次,一般用于上一级点击进入下一级,需要传参数时使用。onShow除了页面第一次加载时会执行,页面有什么小程序API操作行为也会触发执行。onLoad:function(options){//页面初始化options为页面跳转所带来的参数},onShow:function(){…

    2022年6月19日
    149

发表回复

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

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