[Naive Approach] Recursive Inorder Traversal - O(n) Time and O(n) Space
The idea is to traverse BST in inorder traversal. Note that Inorder traversal of BST accesses elements in sorted (or increasing) order.
While traversing, we keep track of count of visited Nodes and keep adding Nodes until the count becomes k.
C++
// C++ program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BST#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=right=nullptr;}};// Recursive function to calculate the sum of the // first k smallest elementsvoidcalculateSum(Node*root,int&k,int&ans){if(root->left!=nullptr){calculateSum(root->left,k,ans);}if(k>0){ans+=root->data;k--;}else{return;}if(root->right!=nullptr){calculateSum(root->right,k,ans);}}// Function to find the sum of the first // k smallest elementsintsum(Node*root,intk){intans=0;calculateSum(root,k,ans);returnans;}intmain(){// Input BST// 8// / \ // 7 10// / / \ // 2 9 13Node*root=newNode(8);root->left=newNode(7);root->right=newNode(10);root->left->left=newNode(2);root->right->left=newNode(9);root->right->right=newNode(13);intk=3;cout<<sum(root,k)<<"\n";return0;}
C
// C program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BST#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// Recursive function to calculate the sum of// the first k smallest elementsvoidcalculateSum(structNode*root,int*k,int*ans){if(root->left!=NULL){calculateSum(root->left,k,ans);}if(*k>0){*ans+=root->data;(*k)--;}else{return;}if(root->right!=NULL){calculateSum(root->right,k,ans);}}// Function to find the sum of the first // k smallest elementsintsum(structNode*root,intk){intans=0;calculateSum(root,&k,&ans);returnans;}structNode*newNode(intdata){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=data;node->left=node->right=NULL;returnnode;}intmain(){// Input BST// 8// / \ // 7 10// / / \ // 2 9 13structNode*root=newNode(8);root->left=newNode(7);root->right=newNode(10);root->left->left=newNode(2);root->right->left=newNode(9);root->right->right=newNode(13);intk=3;printf("%d\n",sum(root,k));return0;}
Java
// Java program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BSTclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// Recursive function to calculate the sum of the // first k smallest elementsstaticvoidcalculateSum(Noderoot,int[]k,int[]ans){if(root.left!=null){calculateSum(root.left,k,ans);}if(k[0]>0){ans[0]+=root.data;k[0]--;}else{return;}if(root.right!=null){calculateSum(root.right,k,ans);}}// Function to find the sum of the first// k smallest elementsstaticintsum(Noderoot,intk){int[]ans={0};int[]kArr={k};calculateSum(root,kArr,ans);returnans[0];}publicstaticvoidmain(String[]args){// Input BST// 8// / \// 7 10// / / \// 2 9 13Noderoot=newNode(8);root.left=newNode(7);root.right=newNode(10);root.left.left=newNode(2);root.right.left=newNode(9);root.right.right=newNode(13);intk=3;System.out.println(sum(root,k));}}
Python
# Python3 program to find Sum Of All # Elements smaller than or equal to # Kth Smallest Element In BST classNode:def__init__(self,data):self.data=dataself.left=Noneself.right=None# Recursive function to calculate the sum of # the first k smallest elementsdefcalculateSum(root,k,ans):ifroot.leftisnotNone:calculateSum(root.left,k,ans)ifk[0]>0:ans[0]+=root.datak[0]-=1else:returnifroot.rightisnotNone:calculateSum(root.right,k,ans)# Function to find the sum of the first k # smallest elementsdefsum(root,k):ans=[0]calculateSum(root,[k],ans)returnans[0]if__name__=="__main__":# Input BST# 8# / \# 7 10# / / \# 2 9 13root=Node(8)root.left=Node(7)root.right=Node(10)root.left.left=Node(2)root.right.left=Node(9)root.right.right=Node(13)k=3print(sum(root,k))
C#
// C# program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BSTusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intdata){this.data=data;left=right=null;}}classGfG{// Recursive function to calculate the sum of // the first k smallest elementsstaticvoidCalculateSum(Noderoot,refintk,refintans){if(root.left!=null){CalculateSum(root.left,refk,refans);}if(k>0){ans+=root.data;k--;}else{return;}if(root.right!=null){CalculateSum(root.right,refk,refans);}}// Function to find the sum of the first k // smallest elementsstaticintsum(Noderoot,intk){intans=0;CalculateSum(root,refk,refans);returnans;}staticvoidMain(){// Input BST// 8// / \// 7 10// / / \// 2 9 13Noderoot=newNode(8);root.left=newNode(7);root.right=newNode(10);root.left.left=newNode(2);root.right.left=newNode(9);root.right.right=newNode(13);intk=3;Console.WriteLine(sum(root,k));}}
JavaScript
// Javascript program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BSTclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}// Recursive function to calculate the sum of the // first k smallest elementsfunctioncalculateSum(root,k,ans){if(root.left!==null){calculateSum(root.left,k,ans);}if(k.val>0){ans.val+=root.data;k.val--;}else{return;}if(root.right!==null){calculateSum(root.right,k,ans);}}// Function to find the sum of the first k // smallest elementsfunctionsum(root,k){letans={val:0};letkObj={val:k};calculateSum(root,kObj,ans);returnans.val;}// Input BST// 8// / \// 7 10// / / \// 2 9 13letroot=newNode(8);root.left=newNode(7);root.right=newNode(10);root.left.left=newNode(2);root.right.left=newNode(9);root.right.right=newNode(13);letk=3;console.log(sum(root,k));
Output
17
Time complexity: O(n), where n is the number of nodes in the Binary Search Tree, as the algorithm performs an inorder traversal visiting each node once. Auxiliary Space: O(h),where h is the height of the tree, due to the recursive call stack. In the worst case (skewed tree), it can be O(n).
[Expected Approach] Morris Traversal - O(n) Time and O(1) Space
The idea is to use Morris Traversal , this method establishes temporary threads (links) to allow traversal and reverts these changes afterward to restore the original tree structure.
By counting nodes during traversal, we can compute the cumulative sum of the k nodes visited.
Follow the steps below to solve the problem:
Initialize current as root, and counter and result to store the count and sum of elements found, respectively.
If current has no left child, increment counter and add current.data to result.
If counter == k, return result.
Move to the right by updating current = current.right.
Otherwise, find the rightmost node in current's left subtree (inorder predecessor) or a node whose right child is current.
If the right child of the found node is current, restore the original tree by setting the right child of the node to NULL, increment counter, and add current.data to result.
If counter == k, return result.
Move to the right by updating current = current.right.
Otherwise, set current as the right child of the rightmost node and move current = current.left.
C++
// C++ program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BST// Using Morris Traversal #include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=right=nullptr;}};// Function to find the sum of all elements // smaller than or equal to k-th smallest elementintsum(Node*root,intk){Node*current=root;intcount=0,result=0;while(current!=nullptr){if(current->left==nullptr){// Visit this nodecount++;result+=current->data;if(count==k){returnresult;}// Move to the right subtreecurrent=current->right;}else{// Find the predecessor// (rightmost node in left subtree)Node*pre=current->left;while(pre->right!=nullptr&&pre->right!=current){pre=pre->right;}if(pre->right==nullptr){// Establish thread/link from // predecessor to currentpre->right=current;// Move to the left subtreecurrent=current->left;}else{// Revert the thread/link from// predecessor to currentpre->right=nullptr;// Visit this nodecount++;result+=current->data;if(count==k){returnresult;}// Move to the right subtreecurrent=current->right;}}}returnresult;}intmain(){// Input BST// 8// / \ // 7 10// / / \ // 2 9 13Node*root=newNode(8);root->left=newNode(7);root->right=newNode(10);root->left->left=newNode(2);root->right->left=newNode(9);root->right->right=newNode(13);intk=3;cout<<sum(root,k)<<"\n";return0;}
C
// C program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BST// Using Morris Traversal#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left,*right;};// Function to find the sum of all elements// smaller than or equal to k-th smallest elementintsum(structNode*root,intk){structNode*current=root;intcount=0,result=0;while(current!=NULL){if(current->left==NULL){// Visit this nodecount++;result+=current->data;if(count==k){returnresult;}// Move to the right subtreecurrent=current->right;}else{// Find the predecessor // (rightmost node in left subtree)structNode*pre=current->left;while(pre->right!=NULL&&pre->right!=current){pre=pre->right;}if(pre->right==NULL){// Establish thread/link from // predecessor to currentpre->right=current;// Move to the left subtreecurrent=current->left;}else{// Revert the thread/link from // predecessor to currentpre->right=NULL;// Visit this nodecount++;result+=current->data;if(count==k){returnresult;}// Move to the right subtreecurrent=current->right;}}}returnresult;}structNode*createNode(intdata){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->left=newNode->right=NULL;returnnewNode;}intmain(){// Input BST// 8// / \ // 7 10// / / \ // 2 9 13structNode*root=createNode(8);root->left=createNode(7);root->right=createNode(10);root->left->left=createNode(2);root->right->left=createNode(9);root->right->right=createNode(13);intk=3;printf("%d\n",sum(root,k));return0;}
Java
// Java program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BST// Using Morris TraversalclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// Function to find the sum of all elements // smaller than or equal to k-th smallest elementstaticintsum(Noderoot,intk){Nodecurrent=root;intcount=0,result=0;while(current!=null){if(current.left==null){// Visit this nodecount++;result+=current.data;if(count==k){returnresult;}// Move to the right subtreecurrent=current.right;}else{// Find the predecessor // (rightmost node in left subtree)Nodepre=current.left;while(pre.right!=null&&pre.right!=current){pre=pre.right;}if(pre.right==null){// Establish thread/link from // predecessor to currentpre.right=current;// Move to the left subtreecurrent=current.left;}else{// Revert the thread/link from// predecessor to currentpre.right=null;// Visit this nodecount++;result+=current.data;if(count==k){returnresult;}// Move to the right subtreecurrent=current.right;}}}returnresult;}publicstaticvoidmain(String[]args){// Input BST// 8// / \// 7 10// / / \// 2 9 13Noderoot=newNode(8);root.left=newNode(7);root.right=newNode(10);root.left.left=newNode(2);root.right.left=newNode(9);root.right.right=newNode(13);intk=3;System.out.println(sum(root,k));}}
Python
# Python program to find Sum Of All Elements smaller# than or equal to Kth Smallest Element In BST# Using Morris TraversalclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=None# Function to find the sum of all elements # smaller than or equal to k-th smallest elementdefsum(root,k):current=rootcount=0result=0whilecurrent:ifcurrent.leftisNone:# Visit this nodecount+=1result+=current.dataifcount==k:returnresult# Move to the right subtreecurrent=current.rightelse:# Find the predecessor # (rightmost node in left subtree)pre=current.leftwhilepre.rightisnotNoneandpre.right!=current:pre=pre.rightifpre.rightisNone:# Establish thread/link from # predecessor to currentpre.right=current# Move to the left subtreecurrent=current.leftelse:# Revert the thread/link from # predecessor to currentpre.right=None# Visit this nodecount+=1result+=current.dataifcount==k:returnresult# Move to the right subtreecurrent=current.rightreturnresultif__name__=="__main__":# Input BST# 8# / \# 7 10# / / \# 2 9 13root=Node(8)root.left=Node(7)root.right=Node(10)root.left.left=Node(2)root.right.left=Node(9)root.right.right=Node(13)k=3print(sum(root,k))
C#
// C# program to find Sum Of All Elements smaller// than or equal to Kth Smallest Element In BST// Using Morris TraversalusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// Function to find the sum of all elements // smaller than or equal to k-th smallest elementstaticintsum(Noderoot,intk){Nodecurrent=root;intcount=0,result=0;while(current!=null){if(current.left==null){// Visit this nodecount++;result+=current.data;if(count==k){returnresult;}// Move to the right subtreecurrent=current.right;}else{// Find the predecessor // (rightmost node in left subtree)Nodepre=current.left;while(pre.right!=null&&pre.right!=current){pre=pre.right;}if(pre.right==null){// Establish thread/link from // predecessor to currentpre.right=current;// Move to the left subtreecurrent=current.left;}else{// Revert the thread/link from // predecessor to currentpre.right=null;// Visit this nodecount++;result+=current.data;if(count==k){returnresult;}// Move to the right subtreecurrent=current.right;}}}returnresult;}staticvoidMain(string[]args){// Input BST// 8// / \// 7 10// / / \// 2 9 13Noderoot=newNode(8);root.left=newNode(7);root.right=newNode(10);root.left.left=newNode(2);root.right.left=newNode(9);root.right.right=newNode(13);intk=3;Console.WriteLine(sum(root,k));}}
JavaScript
// JavaScript program to find Sum Of All Elements // smaller than or equal to Kth Smallest // Element In BST Using Morris TraversalclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}// Function to find the sum of all elements // smaller than or equal to k-th smallest elementfunctionsum(root,k){letcurrent=root;letcount=0;letresult=0;while(current!==null){if(current.left===null){// Visit this nodecount++;result+=current.data;if(count===k){returnresult;}// Move to the right subtreecurrent=current.right;}else{// Find the predecessor (rightmost node in left subtree)letpre=current.left;while(pre.right!==null&&pre.right!==current){pre=pre.right;}if(pre.right===null){// Establish thread/link from predecessor to currentpre.right=current;// Move to the left subtreecurrent=current.left;}else{// Revert the thread/link from predecessor to currentpre.right=null;// Visit this nodecount++;result+=current.data;if(count===k){returnresult;}// Move to the right subtreecurrent=current.right;}}}returnresult;}// Input BST// 8// / \// 7 10// / / \// 2 9 13letroot=newNode(8);root.left=newNode(7);root.right=newNode(10);root.left.left=newNode(2);root.right.left=newNode(9);root.right.right=newNode(13);letk=3;console.log(sum(root,k));
Output
17
Time Complexity: O(k), since we only traverse the tree until the k-th smallest element. Auxiliary Space:O(1), for the iterative approach, as it uses a constant amount of space, with no additional data structures aside from a few variables.