Leetcode-337 House Robber III

科技中介如何高效匹配技术供需?.docx 科技中介如何高效匹配技术供需? 立即下载

337. House Robber III

Medium

176635FavoriteShare

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.

/**
 * 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;
        }
        if(root->left == NULL && root->right == NULL){
            return root->val;
        }
        int l = rob(root->left);
        int r = rob(root->right);
        int ll,lr,rl,rr;
        if(root->left && root->left->left){
            ll = rob(root->left->left);
        }
        else{
            ll = 0;
        }
        if(root->left && root->left->right){
            lr = rob(root->left->right);
        }
        else{
            lr = 0;
        }
        if(root->right && root->right->left){
            rl = rob(root->right->left);
        }
        else{
            rl = 0;
        }
        if(root->right && root->right->right){
            rr = rob(root->right->right);
        }
        else{
            rr = 0;
        }
        
        return max(l+r, root->val+ll+lr+rr+rl);
        
        
    }
    
    int max(int a, int b){
        if(a>b){
            return a;
        }
        else{
            return b;
        }
    }
};

 

一直在理解错题意,首先是觉得答案应该是偶数行的value或者奇数行的value总和。

后来甚至开始写背包问题,每行的value和为背包中的元素,算最大值

但是忘记考虑了,相邻行但不是父子关系的也可以

其实就应该用递归来解决问题,在highlevel上思考就好了。

!qhyb.rar 当 CAD 缺失对应字体时,图纸文字会显示异常,出现乱码、问号。将下载好的字体文件复制到 AutoCAD 的 Fonts 文件夹中,即可恢复正常显示。 立即下载

相关推荐

政府科技管理部门如何实现科技成果高效转化?.docx

政府科技管理部门如何实现科技成果高效转化?

LeetCode //C - 337. House Robber III

【代码】LeetCode //C - 337. House Robber III

Made in Code 1083

librdkafka Windows 包(可直接使用)

librdkafka/win ├─ bin/ # 全部运行时dll + pdb调试符号 + plugins插件目录 ├─ include/ # 头文件 rdkafka.h └─ lib/ # .lib静态导入库(链接阶段使用)

[leetcode]337. House Robber III

[leetcode]337. House Robber III Analysis fighting!!!—— [每天刷题并不难0.0] The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besi...

weixin_32135877的博客 181

Leetcode337. House Robber III

打家劫舍的树形版本,限制条件是相连的节点不能一起偷

magic_jiayu的博客 203

[leetcode] 337. House Robber III

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

Learning from the mistakes 123

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

题目链接: https://leetcode.com/problems/house-robber-iii/ 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,

小榕流光的专栏 782

LeetCode337. House Robber III

337. House Robber III Description: 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...

Freemanzxp 347

LeetCode337. House Robber III

337House Robber III 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 par...

grllery的博客 421

[LeetCode 337] House Robber III

题目内容 House Robber IIITotal Accepted: 29353 Total Submissions: 71912 Difficulty: Medium Contributors: Admin The thief has found himself a new place for his thievery again. There is only one entrance

yangjin95的博客 290

国有企业如何推动科技创新与产业升级融合?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

科技中介服务如何实现数字化升级?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

兄弟MFC-J2330DW 中文维修手册

兄弟MFC-J2330DW 中文维修手册

CesiumForUnitySamples-v1.25.1

Cesium for Unity 是一个官方插件,它将 Cesium 强大的3D 地理空间平台能力深度集成到了 Unity 引擎中。简单来说,它让你能在 Unity 里创建出一个高精度的真实地球,并加载和探索全球范围内的海量地理数据

政府科技管理部门如何降低平台建设成本?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

sam l part3 niubi666

sam l part3 niubi666

内窥镜摄像系统,全球前10强生产商排名及市场份额.docx

内窥镜摄像系统,全球前10强生产商排名及市场份额

上一篇: Leetcode-238 Product of Array Except Self
下一篇: Leetcode-951 Flip Equivalent Binary Trees
Andreadyx
博客等级 码龄11年 0粉丝 13原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值