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

 1 struct Money {
 2         int pre;
 3         int curr;
 4         Money():pre(0), curr(0){}
 5     };
 6
 7     int rob(TreeNode* root) {
 8         Money sum = dfs(root);
 9         return sum.curr;
10     }
11
12     Money dfs(TreeNode* root)
13     {
14         if (root == NULL) return Money();
15         Money leftMoney = dfs(root->left);
16         Money rightMoney = dfs(root->right);
17         Money sumMoney;
18         sumMoney.pre = leftMoney.curr + rightMoney.curr; // 当前节点不偷
19         sumMoney.curr = max(sumMoney.pre, root->val + leftMoney.pre + rightMoney.pre);
20         return sumMoney;
21     }
时间: 2024-10-10 16:53:35

[leetcode] 337.House Robber III的相关文章

LeetCode 337. House Robber III 动态演示

每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{cur, false}]表示以cur为根的树,不偷cur节点的钱,最多能偷的钱 可以看出有下面的关系 mp[{node, false}] = mp[{node->left,true}] + mp[{node->right,true}] mp[{node, true}] = max(node->

<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

<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

【LeetCode】House Robber III(337)

1. 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. After a tour, the smart thief realized tha

LeetCode in Python 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 hou

337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环

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 hou

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 hou

337. House Robber III java solutions

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 hou

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 t