博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 337. 打家劫舍 III(DAY 88) ---- Leetcode Hot 100
阅读量:231 次
发布时间:2019-02-28

本文共 1038 字,大约阅读时间需要 3 分钟。

原题题目

在这里插入图片描述


代码实现(首刷自解 之前没做明白 说明3个月的提升aaa)

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode() : val(0), left(nullptr), right(nullptr) {} *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */class Solution {
public: unordered_map
m; int rob(TreeNode* root) {
if(!root) return 0; if(!root) return 0; int l = rob(root->left); int r = rob(root->right); int ll = 0,lr = 0,rl = 0,rr = 0; if(root->left) {
ll = m[root->left->left]; lr = m[root->left->right]; } if(root->right) {
rl = m[root->right->left]; rr = m[root->right->right]; } if(l + r >= ll + lr + rl + rr + root->val) m[root] = l + r; else m[root] = ll + lr + rl + rr + root->val; return m[root]; }};

转载地址:http://fcni.baihongyu.com/

你可能感兴趣的文章