https://leetcode.com/problems/house-robber-iii/
1. Use recursion:
//8 ms 18.4 MB/*** 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 {public:int rob(TreeNode* root) {int l = 0;int r = 0;return rob(root, l, r);}private:int rob(TreeNode* root, int& l, int& r){if(!root) return 0;int ll = 0;int lr = 0;int rl = 0;int rr = 0;l = rob(root->left, ll, lr);r = rob(root->right, rl, rr);return max(root->val + ll + lr + rl + rr, l + r);}};
