记忆化搜索
/*** 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<TreeNode*, int> umap;int rob(TreeNode* root) {if (root == NULL)return 0;if (root->left == NULL && root->right == NULL)return root->val;if (umap[root])return umap[root];// 偷当前节点int val1 = root->val;if(root->left!=NULL)val1 += rob(root->left->left) + rob(root->left->right);if(root->right!=NULL)val1 += rob(root->right->left) + rob(root->right->right);//不偷当前节点int val2 = rob(root->left)+rob(root->right);umap[root] = max(val1,val2);return max(val1,val2);}
};