14. Sum of Nodes in BST Range
β GFG solution to the Sum of Nodes in BST Range problem: efficiently calculate sum of all nodes within a given range using BST properties. π
π§© Problem Description
π Examples
Example 1
Input: root = [22, 12, 30, 8, 20], l = 10, r = 22
Output: 54
Explanation: The nodes in the given Tree that lies in the range [10, 22] are {12, 20, 22}.
Therefore, the sum of nodes is 12 + 20 + 22 = 54.Example 2
Input: root = [8, 5, 11, 3, 6, N, 20], l = 11, r = 15
Output: 11
Explanation: The nodes in the given Tree that lies in the range [11, 15] is {11}.
Therefore, the sum of node is 11.π Constraints
β
My Approach
Optimized DFS with BST Property
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated