✅☑Beats 97.27% Users || [C++/Java/Python/JavaScript] || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n\n---\n# Approaches\n(Also explained in the code)\n\n1. Function Purpose: The function rangeSumBST calculates the sum of 
# PLEASE UPVOTE IF IT HELPED\n\n---\n![Screenshot 2024-01-08 053346.png](https://assets.leetcode.com/users/images/52945e41-9bb7-4981-9ae8-c6ab5400f147_1704672289.9640503.png)\n\n\n---\n# Approaches\n(Also explained in the code)\n\n1. **Function Purpose:** The function `rangeSumBST` calculates the sum of node values within the given range `[L, R]` in a Binary Search Tree.\n\n1. **Base Case Handling:** If the current node (`root`) is `nullptr` (i.e., the tree/subtree is empty), the function returns `0` as there are no values to sum within an empty tree.\n\n1. **Node Value Comparison:** It checks whether the value of the current node `(root->val)` is within the given range `[L, R]`.\n\n1. **Recursive Sum Calculation:**\n\n    - If the value falls within the range, it includes the current node\'s value in the sum and recursively calls the function for its left and right subtrees.\n    - If the value is less than `L`, it means the current node\'s value and its left subtree\'s values are smaller than `L`, so it focuses only on the right subtree.\n    - If the value is greater than `R`, it means the current node\'s value and its right subtree\'s values are greater than `R`, so it focuses only on the left subtree.\n1. **Returning Sum:** The function recursively computes the sum of values satisfying the given range conditions and returns the total sum of node values within the range `[L, R]`.\n\n\n# Complexity\n- Time complexity:\n   $$O(n)$$\n    \n\n- Space complexity:\n   $$O(h)$$\n(*where H is the height of the tree*)\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    int rangeSumBST(TreeNode* root, int L, int R) {\n        if (root == nullptr) {\n            return 0;\n        }\n        \n        if (root->val >= L && root->val <= R) {\n            return root->val + rangeSumBST(root->left, L, R) + rangeSumBST(root->right, L, R);\n        } else if (root->val < L) {\n            return rangeSumBST(root->right, L, R);\n        } else {\n            return rangeSumBST(root->left, L, R);\n        }\n    }\n}; \n\n\n\n```\n```Java []\nclass Solution {\n    public int rangeSumBST(TreeNode root, int L, int R) {\n        if (root == null) {\n            return 0;\n        }\n        \n        if (root.val >= L && root.val <= R) {\n            return root.val + rangeSumBST(root.left, L, R) + rangeSumBST(root.right, L, R);\n        } else if (root.val < L) {\n            return rangeSumBST(root.right, L, R);\n        } else {\n            return rangeSumBST(root.left, L, R);\n        }\n    }\n}\n\n\n\n```\n```python3 []\nclass Solution:\n    def rangeSumBST(self, root, L, R):\n        if not root:\n            return 0\n        \n        if L <= root.val <= R:\n            return root.val + self.rangeSumBST(root.left, L, R) + self.rangeSumBST(root.right, L, R)\n        elif root.val < L:\n            return self.rangeSumBST(root.right, L, R)\n        else:\n            return self.rangeSumBST(root.left, L, R)\n\n\n\n```\n```javascript []\nvar rangeSumBST = function(root, L, R) {\n    if (!root) {\n        return 0;\n    }\n    \n    if (root.val >= L && root.val <= R) {\n        return root.val + rangeSumBST(root.left, L, R) + rangeSumBST(root.right, L, R);\n    } else if (root.val < L) {\n        return rangeSumBST(root.right, L, R);\n    } else {\n        return rangeSumBST(root.left, L, R);\n    }\n};\n\n\n\n```\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Tree', 'Depth-First Search', 'Binary Search Tree', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']