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

House Robber(打家劫舍问题) 待补充 阅读详情

题目:

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.

思路:

二叉树一般情况下用递归是最好的办法。分析本题目可知,要想获得最大值有两种选择:

1)抢劫根节点,那么此时就不能抢劫左右子树的根节点;

2)不抢劫根节点,那么此时就可以抢劫左右子树的根节点。

两种情况中取受益最大者即可。在实现中值得注意的是,在递归过程中有可能会引起重复计算。例如代码片段1中,我们在rob(root->left)的调用过程中会调用rob(root->left->left),而这个调用在rob(root)中也出现,导致大量重复计算。一种比较好的递归方式见代码片段2,我们采用“首递归”,并用传递引用的方式记录左右子树已经取得的最大值,可以完美地避免重复计算。(测试结果表明代码片段2的效率比代码片段1的效率高100倍左右)

代码:

1、低效递归:

/**
 * 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) {
            return 0;
        }
        int rob_root = root->val;
        if (root->left) {
            rob_root += rob(root->left->left);
            rob_root += rob(root->left->right);
        }
        if (root->right) {
            rob_root += rob(root->right->left);
            rob_root += rob(root->right->right);
        }
        int not_rob_root = rob(root->left);
        not_rob_root += rob(root->right);
        return max(rob_root, not_rob_root);
    }
};

2、高效递归:

/**
 * 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) {
        int left = 0, right = 0;
        return rob(root, left, right);
    }
private:
    int rob(TreeNode* root, int &left, int &right) {
        if(!root) {
            return 0;
        }
        int left_left = 0, left_right = 0, right_left = 0, right_right = 0;
        left = rob(root->left, left_left, left_right);      // left_left and left_right will be updated during recursions
        right = rob(root->right, right_left, right_right);  // right_left and right_right will be updated during recursions    
        return max(root->val + left_left + left_right + right_left + right_right, left + right);
    }
};
从OpenClaw到Hermes Agent:AI Agent实战部署与自动化工作流构建 AI Agent(智能体)作为大语言模型(LLM)的能力延伸,其核心原理在于通过工具调用(Tool Calling)与环境交互,使模型从“对话”走向“执行”。这一技术价值在于将自然语言指令转化为具体操作,实现业务流程自动化,从而提升开发与运维效率。在应用场景上,AI Agent广泛适用于自动化运维、智能客服、数据报告生成等重复性任务处理。本文聚焦于两大热门开源框架OpenClaw与Hermes Agent,深入探讨其部署配置、技能扩展及实战联调。通过结合OpenClaw的模块化扩展能力与Hermes Age 阅读详情

相关推荐

windows中如何将已安装的nodejs高版本降级为低版本

第一步:先清空本地安装的node.js版本 1.按健win+R弹出窗口,键盘输入cmd,然后敲回车(或者鼠标直接点击电脑左面最左下角的win窗口图标弹出,输入cmd再点击回车键) 然后进入命令控制行窗口,并输入where node查看之前本地安装的node的路径 2.找到上面找到的路径,将node.exe所在的父目录里面的所有东西都删除 3.为了彻底删除之前安装的node.js,鼠标点击电脑左面最左下角的win窗口图标弹出,输入“控制面板”再点击回车键进入,进入控制面板后,找到所安装的node.js并

weixin_44582077的博客 9万+

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, each house has one and only one parent house. After

xiaohuihui52309的专栏 279

手把手教你用STM32F030实现功率MOS双脉冲测试(附完整电路图)

本文详细介绍了如何使用STM32F030微控制器搭建功率MOSFET双脉冲测试平台。通过分析双脉冲测试原理,提供了完整的硬件电路设计、驱动电路选型、软件时序生成方法,并指导如何利用示波器捕获和分析开关波形,从而评估功率器件的动态特性与开关损耗。

juice的博客 726

LeetCode 337. House Robber III 题解

337. House Robber III   Add to List Question Editorial Solution  My Submissions Total Accepted: 30247Total Submissions: 73770Difficulty: MediumContributors: Admin Th

daqiang95的博客 439

[Ethernet in CANoe]2--如何在CANoe中去仿真CP版本的SOME/IP通信

上篇文章()介绍了SOME/IP的两种类型的数据格式,根据作者目前的项目经验,目前在CANoe V18之前的版本,CP版本的arxml是最方便实现SOME/IP以太网仿真的。作者目前只有CANoe V17的license,所以本片文章是在CANoe 17上基于CP版本的arxml去做仿真模拟。

汽车ECU自动化测试 1777

[Python从零到壹] 十四.机器学习之分类算法五万字总结全网首发(决策树、KNN、SVM、分类对比实验)

分类(Classification)属于有监督学习(Supervised Learning)中的一类,它是数据挖掘、机器学习和数据科学中一个重要的研究领域。分类模型类似于人类学习的方式,通过对历史数据或训练集的学习得到一个目标函数,再用该目标函数预测新数据集的未知属性。本章主要讲述分类算法基础概念,并结合决策树、KNN、SVM分类算法案例分析各类数据集,从而让读者学会使用Python分类算法分析自己的数据集,研究自己领域的知识,从而创造价值。 19.1 分类 19.1.1 分类模型 与前面讲述的聚类模型类似

杨秀璋的专栏 2万+

[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,

小榕流光的专栏 785

深入理解 HTTP 协议中的 Content-Type

是 HTTP 协议中的一个头部字段(Header),用于指示请求或响应中实体主体(body)的媒体类型(Media Type),也称为MIME 类型(Multipurpose Internet Mail Extensions)。

h a 6 6 6 c k 为 你解决复杂难题! 1010

LeetCode 337. House Robber III 解题报告

LeetCode 337. House Robber III 解题报告

骆小坑的填坑之旅 846

基于FMU的Star CCM+与Amesim复杂控制联合分析

基于FMU的Star CCM+与Amesim复杂控制联合分析

2201_75375391的博客 2026

【Mode】Mode expansion monitors

对于宽带仿真,因为被支持的模式可能是波长的函数,因此,推荐在波长范围内增加频率的采样点数量去计算支持的波导模式分布或者光纤模式分布。对于宽带仿真,如果在被计算模式上的频率点的数量与通过 frequency-domain monitors 被测量的频率点的数量不匹配,这种情况是正常的。Mode expansion monitors 对于仿真偏振转换器是十分有用的,比如,在偏振转换器中,当你想要提取从输入模式到输出模式的转换效率,或者提取一个装置的 S 参数。给出了进入到被选择模式中的合透射率,

u011699626的博客 2776

【LeetCode】337. House Robber III 解题报告(Python)

【LeetCode】337. House Robber III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/house-robber-ii...

负雪明烛 3575

在 SAP BTP ABAP 环境中测试多租户 SaaS 解决方案的完整实践指南

ABAP SaaS测试阶段关键实践 在SAP BTP ABAP环境中开发多租户SaaS解决方案时,测试阶段(TST)对确保产品质量尤为关键。测试阶段应包含两个主要维度: 业务功能验证: 通过软件组件管理确保测试环境与代码仓库同步 创建并验证业务角色分配,确保权限配置正确 测试Fiori Launchpad导航流程和跨应用跳转 验证inbound/outbound通信配置 自动化质量管控: 在开发环境(DEV)执行本地ATC检查和单元测试 通过ABAP Test Cockpit(ATC)进行代码质量检查 集成

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 306

【LeetCode】337.House Robber III(Medium)解题报告

【LeetCode】337.House Robber III(Medium)解题报告题目地址:https://leetcode.com/problems/house-robber-iii/description/ 题目描述:   The thief has found himself a new place for his thievery again. There is only one e

郝春雨的博客 291

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode //C - 337. House Robber III

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

Made in Code 1098

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

【LeetCode】337. 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 349

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode337. House Robber III

337. 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, each house has one and only one par...

grllery的博客 433

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,

James Pan 1102

LeetCode-337. House Robber III(JAVA)(树形结构)

LeetCode-337. House Robber III(JAVA)(树形结构)

mine_song的博客 701

Leetcode 337. House Robber III(python+cpp)

Leetcode 337. 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, each h...

qq_37821701的博客 536

Leetcode337. House Robber III

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

magic_jiayu的博客 208

Leetcode-337 House Robber III

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

Andreadyx的博客 205

House Robber Leetcode 198,213,337

House Robber Leetcode 题目描述 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。 题目分析 动态规划。令 ans[i]ans[i]ans[i]...

qq_36835991的博客 421

[LeetCode] 337. House Robber III

原题链接: https://leetcode.com/problems/house-robber-iii/ House Robber 专题相关题目 198. House Robber 213. House Robber II [337. House Robber III] 1. 题目介绍 The thief has found himself a new place for his thiever...

Ber03的博客 335

Leetcode 337. 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, each house has one and only one parent house. After a tour...

salmonwilliam的博客 148

【leetcode】【medium】337. House Robber III​​​​​​​

337. 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, each house has one and only one...

lemonade13的博客 143
上一篇: [Leetcode] 336. Palindrome Pairs 解题报告
下一篇: [Leetcode] 338. Counting Bits 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值