🔥Python3🔥 DFS (One-liner || Recursive || Iterative Explained)
One-liner\npython\nclass Solution:\n    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:\n        return 0 if not root else self.ran
**One-liner**\n```python\nclass Solution:\n    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:\n        return 0 if not root else self.rangeSumBST(root.right,low,high) + self.rangeSumBST(root.left,low,high) + int(low<=root.val<=high) * root.val\n```\n\n**Recursive 1** (more readable)\n```python\nclass Solution:\n    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:\n        \n        if not root: \n            return 0\n        \n        # When value is less than low, everything on it\'s left doesn\'t matter, \n        # so only return the sum from its right children\n        if root.val<low: \n            return self.rangeSumBST(root.right,low,high)\n        \n        # Same thing for high.\n        elif root.val>high: \n            return self.rangeSumBST(root.left,low,high)\n        \n        # The current value is in the range, so return the sum of its left, right and own value\n        else:\n            return root.val + self.rangeSumBST(root.right,low,high) + self.rangeSumBST(root.left,low,high)\n```\n\n**Recursive 2**\n```python\nclass Solution:\n    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:\n        res = 0\n        def dfs(node):\n            nonlocal res\n            if not node: return\n            \n            # increase res only when the node value is in the range\n            if low<=node.val<=high: res += node.val\n            \n            # The only time we don\'t want to go left, is when the nodel value <= low.\n            # Because it is a BST, and if the current value is aleady <= low, \n            # there is no more hope!\n            if node.val>low: dfs(node.left)\n            \n            # Same as above, but going right\n            if node.val<high: dfs(node.right)\n\n        dfs(root)\n        return res\n```\n\n**Iterative** (same idea as Recursive 2)\n```python\nclass Solution:\n    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:\n        res = 0\n        q = [root]\n        while q:\n            cur = q.pop()\n            if cur:\n                if low <= cur.val <= high:\n                    res += cur.val\n                if cur.val > low:\n                    q.append(cur.left)\n                if cur.val < high:\n                    q.append(cur.right)\n        return res\n```\n\n**Upvote** if you like this post.\n\n**Connect with me on [LinkedIn](https://www.linkedin.com/in/meida-chen-938a265b/)** if you\'d like to discuss other related topics\n\n\uD83C\uDF1F If you are interested in **Machine Learning** || **Deep Learning** || **Computer Vision** || **Computer Graphics** related projects and topics, check out my **[YouTube Channel](https://www.youtube.com/@meidachen8489)**! Subscribe for regular updates and be part of our growing community.
['Python']