Easy Explained | Recursion | Optimized | 2 line Code | CPP
Approach\nWe are doing regular tree traversal and adding values within [L, R]. Note that in the solution description, we are given BST, however, this solution w
# Approach\nWe are doing regular tree traversal and adding values within [L, R]. Note that in the solution description, we are given BST, however, this solution works for any binary tree.\n\n# CPP 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://Thanks to Bhalerao-2002\nint rangeSumBST(TreeNode* root, int L, int R) {\n  if (root == nullptr) return 0;//If tree is not Exist \n  return (root->val >= L && root->val <= R ? root->val : 0) +\n    rangeSumBST(root->left, L, R)/*For Left Side traversal of BST*/ + rangeSumBST(root->right, L, R)/*For Right Side traversal of BST*/;\n}\n};\n```\n# Upvote If it Helped :)
['C++']