Easy approach
Intuitionits very effective solution and easy to understand. (first time posting solution)Approachusing dynamic programming to calculate each nodesComplexity

T
# Intuition
its very effective solution and easy to understand. (first time posting solution)

# Approach
using dynamic programming to calculate each nodes

# Complexity
- Time complexity:
Its very fast

- Space complexity:
idk

# Code
```cpp []
/**
 * 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:
    pair<int,int> rec(TreeNode* root){
        pair<int,int> x={0,0},y={0,0},z={0,0};
        if (root->left==NULL && root->right==NULL) {z.first=root->val; return z;} 
        if (root->left!=NULL){ x=rec(root->left);}
        if (root->right!=NULL){ y=rec(root->right);}
        z.first=x.second+y.second+root->val;
        z.second=max(x.first,x.second)+max(y.first,y.second);
        return z;
    }
    int rob(TreeNode* root) {
          auto k=rec(root);  
             return max(k.first,k.second); 
      }
};
```
['Dynamic Programming', 'C++']