Beats 90% O(N) time ✅✅
Approach\nApproach is written in the code in the form of comments just go through them and dry run once , i gaurentee you will understand the logic... I can als
# Approach\nApproach is written in the code in the form of comments just go through them and dry run once , i gaurentee you will understand the logic... I can also explain the logic in brief \nSo here goes the logic , when we add the root data then we can\'t add the adjacent values so we will skip them and when we will not add the root data then we have choice whether we want to add adjacent values or not...\n\n# Complexity\n\n- Time complexity: O(N) as we are traversing all the nodes only once\n\n- Space complexity: O(height of tree)\n\n# **If you liked the solution then pls upvote the solution \uD83E\uDD79**\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n    pair<int,int> solve(TreeNode *root){\n        // base case\n        if(root == NULL){\n            return {0,0};\n        }\n\n        // left part ki call\n        pair<int,int> leftAns = solve(root->left);\n\n        // right part ki call\n        pair<int,int> rightAns = solve(root->right);\n\n        // creating final ans \n        pair<int,int>ans;\n\n        // jab humne root ke data koh include kara, toh adjacent koh pick nhi kr skte\n        ans.first = root->val + leftAns.second + rightAns.second;\n\n        // jab humne root ke data koh include nhi kara \n        ans.second = max(leftAns.first, leftAns.second) + max(rightAns.first, rightAns.second);\n\n        return ans;\n    }\n\n\n    int rob(TreeNode* root) {\n        // we can think of this problem ans we want to add the root value or not\n        pair<int,int>ans = solve(root);\n        return max(ans.first, ans.second);\n    }\n};\n```
['C++']