Given the root of a Binary Search Tree containing n nodes, and two integers l and r, find the sum of all node values that lie within the inclusive range [l, r].
Examples:
Input: 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.
Input: 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.
[Approach] - Traverse the tree and check if node lies in range
The idea to traverse the tree recursively. For each node, if it lies within the range, then return the sum of left subtree, right subtree and current node. If its value is less than range, then return the value of right subtree, because all the nodes in its left subtree are less than than the range. Otherwise return the value of left subtree, because all the nodes in its right subtree are greater than the range.
C++
//Driver Code Starts#include<iostream>usingnamespacestd;// Node strcutureclassNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};//Driver Code EndsintnodeSum(Node*root,intl,intr){if(root==nullptr)return0;// If root value is less than range// all nodes in its left subtree // will be less than rangeif(root->data<l){returnnodeSum(root->right,l,r);}// If root value is greater than range.// all nodes in its right subtree // will be greater than rangeelseif(root->data>r){returnnodeSum(root->left,l,r);}// If root value lies in the range.intleft=nodeSum(root->left,l,r);intright=nodeSum(root->right,l,r);returnleft+right+root->data;}//Driver Code Startsintmain(){// BST// 22// / \ // 12 30// / \ // 8 20Node*root=newNode(22);root->left=newNode(12);root->right=newNode(30);root->left->left=newNode(8);root->left->right=newNode(20);intl=10,r=22;cout<<nodeSum(root,l,r)<<endl;return0;}//Driver Code Ends
Java
//Driver Code Startsimportjava.util.ArrayList;// Node strcutureclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGFG{//Driver Code EndsstaticintnodeSum(Noderoot,intl,intr){if(root==null)return0;// If root value is less than range.// all nodes in its left subtree // will be less than rangeif(root.data<l){returnnodeSum(root.right,l,r);}// If root value is greater than range.// all nodes in its right subtree // will be greater than rangeelseif(root.data>r){returnnodeSum(root.left,l,r);}// If root value lies in the range.intleft=nodeSum(root.left,l,r);intright=nodeSum(root.right,l,r);returnleft+right+root.data;}//Driver Code Startspublicstaticvoidmain(String[]args){// BST// 22// / \// 12 30// / \// 8 20Noderoot=newNode(22);root.left=newNode(12);root.right=newNode(30);root.left.left=newNode(8);root.left.right=newNode(20);intl=10,r=22;System.out.println(nodeSum(root,l,r));}}//Driver Code Ends
Python
#Driver Code Starts# Node strcutureclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None#Driver Code EndsdefnodeSum(root,l,r):ifrootisNone:return0# If root value is less than range.# all nodes in its left subtree # will be less than rangeifroot.data<l:returnnodeSum(root.right,l,r)# If root value is greater than range.# all nodes in its right subtree # will be greater than rangeelifroot.data>r:returnnodeSum(root.left,l,r)# If root value lies in the range.left=nodeSum(root.left,l,r)right=nodeSum(root.right,l,r)returnleft+right+root.data#Driver Code Startsif__name__=="__main__":# BST# 22# / \# 12 30# / \# 8 20root=Node(22)root.left=Node(12)root.right=Node(30)root.left.left=Node(8)root.left.right=Node(20)l=10r=22print(nodeSum(root,l,r))#Driver Code Ends
C#
//Driver Code StartsusingSystem;usingSystem.Collections.Generic;// Node strcutureclassNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGFG{//Driver Code EndsstaticintnodeSum(Noderoot,intl,intr){if(root==null)return0;// If root value is less than range.// all nodes in its left subtree // will be less than rangeif(root.data<l){returnnodeSum(root.right,l,r);}// If root value is greater than range.// all nodes in its right subtree // will be greater than rangeelseif(root.data>r){returnnodeSum(root.left,l,r);}// If root value lies in the range.intleft=nodeSum(root.left,l,r);intright=nodeSum(root.right,l,r);returnleft+right+root.data;}//Driver Code StartsstaticvoidMain(string[]args){// BST// 22// / \// 12 30// / \// 8 20Noderoot=newNode(22);root.left=newNode(12);root.right=newNode(30);root.left.left=newNode(8);root.left.right=newNode(20);intl=10,r=22;Console.WriteLine(nodeSum(root,l,r));}}//Driver Code Ends
JavaScript
//Driver Code Starts// Node strcutureclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}//Driver Code EndsfunctionnodeSum(root,l,r){if(root===null)return0;// If root value is less than range.// all nodes in its left subtree // will be less than rangeif(root.data<l){returnnodeSum(root.right,l,r);}// If root value is greater than range.// all nodes in its right subtree // will be greater than rangeelseif(root.data>r){returnnodeSum(root.left,l,r);}// If root value lies in the range.letleft=nodeSum(root.left,l,r);letright=nodeSum(root.right,l,r);returnleft+right+root.data;}//Driver Code Starts// Driver code// BST// 22// / \// 12 30// / \// 8 20letroot=newNode(22);root.left=newNode(12);root.right=newNode(30);root.left.left=newNode(8);root.left.right=newNode(20);letl=10,r=22;console.log(nodeSum(root,l,r));//Driver Code Ends
Output
54
Time Complexity: O(n), where n is the number of nodes in tree. Auxiliary Space: O(h), whereh is the height of tree.