337. House Robber III


https://leetcode-cn.com/problems/house-robber-iii/solution/da-jia-jie-she-iii-by-leetcode-solution/

class Solution {
public:
	unordered_map <TreeNode*, int> f, g;

	void dfs(TreeNode* o) {
		if (!o) {
			return;
		}
		dfs(o->left);
		dfs(o->right);
		f[o] = o->val + g[o->left] + g[o->right];
		g[o] = max(f[o->left], g[o->left]) + max(f[o->right], g[o->right]);
	}

	int rob(TreeNode* o) {
		dfs(o);
		return max(f[o], g[o]);
	}
};


posted @ 2020-08-05 22:04  aaaaassss  阅读(53)  评论(0编辑  收藏  举报