337_house_robber_III

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

解题思路

强烈推荐这个讨论帖,解释非常详细。

文章第一种方法是通用的递归方法,分两种情况讨论:如果包括根,则结果为根的值和孙子树(子树的子树)的结果之和;如果不包括根,则结果为左右子树结果之和。最后返回两种情况的最大者。这种方法的问题在于有很多重复的计算,计算根结点的时候需要子树,孙子树,然而,在计算子树的时候需要计算子树的子树,而这已经在根结点中已经计算过了,造成重复计算。

文章第二种方法则是为了解决第一个方法的不足,引入hash表保存子树计算结果,避免重复计算。

文章第三种方法其实也是沿用方法一思路,分情况讨论,比较关键的是递归同时返回包含根结点和不包含根结点两种情况下的结果,如果将函数拆开递归是不能实现的。相比方法一,该方法比较明确第区分了两种不同的情况,避免重复计算;相比于方法二,思路更清晰,更简洁。

代码

源码链接:https://github.com/lilingyu/337_house_robber_III

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
using namespace std;


struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};


/**
 * 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{

 private:

     vector<int> robSub(TreeNode* root){
        if(NULL == root){
            return vector<int>(2,0);
        }

        vector<int> left = robSub(root->left);
        vector<int> right = robSub(root->right);

        printf("root:\t%d\n", root->val);
        printf("left:\t%d\t%d\n", left[0], left[1]);
        printf("right:\t%d\t%d\n\n", right[0], right[1]);

        vector<int> res(2,0);

        res[0] = max(left[0], left[1]) + max(right[0], right[1]);
        res[1] = root->val + left[0] + right[0];

        return res;
     }



 public:
    int rob(TreeNode* root){
        vector<int> result = robSub(root);

        return max(result[0], result[1]);
    }



 };


 int main()
 {
     Solution solt;
#if 0
     /*
case 0:
    4
    /\
   1 NULL
   /\
  2 NULL
 /
3

     */
     TreeNode* root = new TreeNode(4);
     root->left = new TreeNode(1);
     root->left->left = new TreeNode(2);
     root->left->left->left = new TreeNode(3);
#endif // 0

#if 1

/*
case 1:
  2
  /\
 1 3
  \
   4
*/
    TreeNode* root = new TreeNode(2);
    root->left = new TreeNode(1);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
#endif // 1

     cout<<solt.rob(root);

     return 0;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值