C++ 2 lines
Just 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 an
Just 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\nOf course, we can optimize a bit for the fact that this is BST. I checked the runtime and did not see any difference in the runtime. Both naive and optimized solution gave me 68 ms.\n```\nint rangeSumBST(TreeNode* root, int L, int R) {\n  if (root == nullptr) return 0;\n  return (root->val >= L && root->val <= R ? root->val : 0) +\n    rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);\n}\n```
[]