美文网首页
Lintcode535 House Robber III so

Lintcode535 House Robber III so

作者: 程风破浪会有时 | 来源:发表于2018-05-28 22:27 被阅读0次

【题目描述】

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.

在上次打劫完一条街道之后和一圈房屋之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子组成的区域比较奇怪,聪明的窃贼考察地形之后,发现这次的地形是一颗二叉树。与前两次偷窃相似的是每个房子都存放着特定金额的钱。你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且当相邻的两个房子同一天被打劫时,该系统会自动报警

算一算,如果今晚去打劫,你最多可以得到多少钱,当然在不触动报警装置的情况下。

【题目链接】

www.lintcode.com/en/problem/house-robber-iii/

【题目解析】

此题可以使用递归。

每个节点都有两个选择,即选当前这个节点或者不选当前这个节点。如果选当前节点则不能选左右子节点,如果不选当前节点,则可以选左右子节点。所以对于每个节点,先递归求解其左右节点的值,若不选当前节点,则其最大值为max(选左节点,不选左节点)+max(选右节点,不选右节点);若选当前节点,则其最大值为不选左节点+不选右节点+当前节点值。最后再在这两个里面取较大的那个即可。

【参考答案】

www.jiuzhang.com/solutions/house-robber-iii/

相关文章

  • Lintcode535 House Robber III so

    【题目描述】 The thief has found himself a new place for his th...

  • 2019-04-07

    LeetCode 337. House Robber III Description The thief has ...

  • 动态十八:打家劫舍 III

    题目地址: https://leetcode-cn.com/problems/house-robber-iii/...

  • House Robber III

    题目来源还是房屋抢劫的问题,不过这次改成了二叉树抢劫。然后我不会!!!看了下答案,最简单的方法就是比较所有情况,深...

  • House Robber III

    问题描述 House Robber III一颗二叉树有n个节点,每个节点都有一个权值,现在要求你从中选出任意个节点...

  • 打家劫舍系列 1,2,3

    打家劫舍3https://leetcode-cn.com/problems/house-robber-iii/su...

  • DP真题

    骨骼清奇:LC 62LC 337 House Robber III LC 486 Predict the Win...

  • house-robber-iii

    这道题略有难度,题目链接。题目要求就是求一个二叉树所有节点数值的和的最大值,条件是有边连接的两个节点不能被同时选中...

  • 337. House Robber III

    题目 The thief has found himself a new place for his thieve...

  • Leetcode 337 - House Robber III

    题目: You are a professional robber planning to rob houses ...

网友评论

      本文标题:Lintcode535 House Robber III so

      本文链接:https://www.haomeiwen.com/subject/usckhftx.html