Skip to content

Instantly share code, notes, and snippets.

@coderRohan123
Created January 8, 2024 13:51
Show Gist options
  • Save coderRohan123/90637a4a8722ed680dbe7b42dcb56bd6 to your computer and use it in GitHub Desktop.
Save coderRohan123/90637a4a8722ed680dbe7b42dcb56bd6 to your computer and use it in GitHub Desktop.
This code snippet defines a class Solution with a method rangeSumBST that calculates the sum of all node values in a binary search tree (BST) that fall within a given range (low to high). It uses a depth-first search (DFS) algorithm

Range Sum of Binary Search Tree

Preview:
# Python3 Solution
class Solution:
    def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
        def dfs(node):
            if not node:
                return
            if low <= node.val <= high:
                self.total_sum += node.val
                dfs(node.left)
                dfs(node.right)
            elif node.val < low:
                dfs(node.right)
            elif node.val > high:
                dfs(node.left)

        self.total_sum = 0
        dfs(root)
        return self.total_sum
Associated Context
Type Code Snippet ( .py )
Associated Tags Python3 Solution rangeSumBST function TreeNode class low parameter high parameter dfs method total_sum variable left node right node val property numpy breadth-first-search depth-first-search algorithm Range Sum BST Binary Search Tree TreeNode DFS (Depth-First Search) Recursive Function Tree Traversal In-order Traversal Binary Tree Algorithm
πŸ“ Custom Description YouTube Video Explanation:

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
πŸ’‘ Smart Description This code snippet defines a class Solution with a method rangeSumBST that calculates the total sum of nodes in a binary tree based on their values. The function recursively checks if each node is valid, and then updates the total_sum variable to 0
This code snippet defines a class Solution with a method rangeSumBST that calculates the sum of all node values in a binary search tree (BST) that fall within a given range (low to high). It uses a depth-first search (DFS) algorithm
πŸ”Ž Suggested Searches Python code to calculate sum of binary tree nodes
Python function to find total sum in a binary tree using DFS
How to implement rangeSumBST method in Python for finding bst node
Algorithm to compute the number of BST found in a binary tree with DFS
Python program to count all binded numbers within a binary tree
Python rangeSumBST function with TreeNode input
Python rangeSumBST function with low and high input
Python function to calculate sum of values within a given range in a binary search tree
Python function to find the sum of values in a binary search tree within a specified range
Python code to calculate the sum of values in a binary search tree between two given values
Related Links https://leetcode.com/problems/range-sum-of-bst/discuss/4525845/Beats-100-Depth-First-Search-Explained-with-Video-C%2B%2BJavaPythonJS
https://leetcode.com/problems/range-sum-of-bst/discuss/
https://leetcode.com/problems/range-sum-of-bst/discuss/?currentPage=1&orderBy=hot&query=
https://leetcode.com/problemset/
https://www.pythoncentral.io/how-to-sort-a-list-tuple-or-object-with-sorted-in-python/
https://www.hackerearth.com/practice/data-structures/trees/binary-search-tree/tutorial/
https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
https://www.w3schools.com/python/python_classes.asp
https://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/
https://www.tutorialspoint.com/python_data_structure/python_binary_tree.htm
https://www.python.org/
https://www.geeksforgeeks.org/deletion-in-binary-search-tree/
Related People Rohan Mallick
Sensitive Information No Sensitive Information Detected
Shareable Link https://ca772742-b4e2-4612-af82-29a364c1f0bb.pieces.cloud/?p=8a3c49a228
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment