Below is the implementation of the above approach:
C++
// C++ program to check if a triplet with// given target exists in the BST or not#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=right=nullptr;}};// A utility function to do inorder traversal// of the BST and store values in a vectorvoidinorder(Node*curr,vector<int>&vec){if(curr!=nullptr){inorder(curr->left,vec);vec.push_back(curr->data);inorder(curr->right,vec);}}// Function to check if a triplet with given sum// exists in the BST or notboolcheckForTriplet(Node*root,intsum){// Vector to store the inorder traversal// of the BSTvector<int>arr;// Call inorder() to do the inorder// on the BST and store it in vecinorder(root,arr);// Now, check if any triplet with given sum// exists in the BST or notintl,r;// Now fix the first element one by one and find the// other two elementsfor(inti=0;i<arr.size()-2;i++){// To find the other two elements, start two index// variables from two corners of the array and move// them toward each otherl=i+1;// index of the last elementr=arr.size()-1;while(l<r){// If we find pair with target - arr[i]// then return trueif(arr[l]+arr[r]==sum-arr[i]){returntrue;}elseif(arr[l]+arr[r]<sum-arr[i])l++;elser--;}}returnfalse;}intmain(){// creating the following BST // 50 // / \ // 30 70 // / \ / \ // 20 40 60 80 Node*root=newNode(50);root->left=newNode(30);root->right=newNode(70);root->left->left=newNode(20);root->left->right=newNode(40);root->right->left=newNode(60);root->right->right=newNode(80);intsum=120;if(checkForTriplet(root,sum))cout<<"true";elsecout<<"false";return0;}
Java
// Java program to check if a triplet with// given target exists in the BST or notimportjava.util.*;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// A utility function to do inorder traversal// of the BST and store values in a vectorstaticvoidinorder(Nodecurr,ArrayList<Integer>arr){if(curr!=null){inorder(curr.left,arr);arr.add(curr.data);inorder(curr.right,arr);}}// Function to check if a triplet with given SUM// exists in the BST or notstaticbooleancheckForTriplet(Noderoot,intsum){// Vector to store the inorder traversal// of the BSTArrayList<Integer>arr=newArrayList<>();// Call inorder() to do the inorder// on the BST and store it in arrinorder(root,arr);// Now, check if any triplet with given sum// exists in the BST or notintl,r;// Now fix the first element one by one and find the// other two elementsfor(inti=0;i<arr.size()-2;i++){// To find the other two elements, start two index// variables from two corners of the array and move// them toward each otherl=i+1;// index of the last elementr=arr.size()-1;while(l<r){// If we find pair with target - arr[i] // then return trueif(arr.get(l)+arr.get(r)==sum-arr.get(i)){returntrue;}elseif(arr.get(l)+arr.get(r)<sum-arr.get(i))l++;elser--;}}returnfalse;}publicstaticvoidmain(String[]args){// creating the following BST // 50 // / \ // 30 70 // / \ / \ // 20 40 60 80 Noderoot=newNode(50);root.left=newNode(30);root.right=newNode(70);root.left.left=newNode(20);root.left.right=newNode(40);root.right.left=newNode(60);root.right.right=newNode(80);intsum=120;if(checkForTriplet(root,sum))System.out.println("true");elseSystem.out.println("false");}}
Python
# Python program to check if a triplet with# given target exists in the BST or notclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# A utility function to do inorder traversal# of the BST and store values in a listdefinorder(curr,arr):ifcurrisnotNone:inorder(curr.left,arr)arr.append(curr.data)inorder(curr.right,arr)# Function to check if a triplet with given SUM# exists in the BST or notdefcheckForTriplet(root,sum):# List to store the inorder traversal# of the BSTarr=[]# Call inorder() to do the inorder# on the BST and store it in arrinorder(root,arr)# Now, check if any triplet with given sum# exists in the BST or notforiinrange(len(arr)-2):l=i+1r=len(arr)-1whilel<r:ifarr[l]+arr[r]==sum-arr[i]:returnTrueelifarr[l]+arr[r]<sum-arr[i]:l+=1else:r-=1returnFalseif__name__=="__main__":# creating the following BST # 50 # / \ # 30 70 # / \ / \ # 20 40 60 80 root=Node(50)root.left=Node(30)root.right=Node(70)root.left.left=Node(20)root.left.right=Node(40)root.right.left=Node(60)root.right.right=Node(80)sum=120ifcheckForTriplet(root,sum):print("true")else:print("false")
C#
// C# program to check if a triplet with// given target exists in the BST or notusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// A utility function to do inorder traversal// of the BST and store values in a liststaticvoidInorder(Nodecurr,List<int>arr){if(curr!=null){Inorder(curr.left,arr);arr.Add(curr.data);Inorder(curr.right,arr);}}// Function to check if a triplet with given sum// exists in the BST or notstaticboolCheckForTriplet(Noderoot,intsum){// List to store the inorder traversal// of the BSTList<int>arr=newList<int>();// Call Inorder() to do the inorder// on the BST and store it in arrInorder(root,arr);// Now, check if any triplet with given sum// exists in the BST or notfor(inti=0;i<arr.Count-2;i++){intl=i+1;intr=arr.Count-1;while(l<r){if(arr[l]+arr[r]==sum-arr[i]){returntrue;}elseif(arr[l]+arr[r]<sum-arr[i])l++;elser--;}}returnfalse;}staticvoidMain(){// creating the following BST // 50 // / \ // 30 70 // / \ / \ // 20 40 60 80 Noderoot=newNode(50);root.left=newNode(30);root.right=newNode(70);root.left.left=newNode(20);root.left.right=newNode(40);root.right.left=newNode(60);root.right.right=newNode(80);intsum=120;if(CheckForTriplet(root,sum))Console.WriteLine("true");elseConsole.WriteLine("false");}}
JavaScript
// JavaScript program to check if a triplet with// given target exists in the BST or notclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// A utility function to do inorder traversal// of the BST and store values in an arrayfunctioninorder(curr,arr){if(curr!==null){inorder(curr.left,arr);arr.push(curr.data);inorder(curr.right,arr);}}// Function to check if a triplet with given SUM// exists in the BST or notfunctioncheckForTriplet(root,sum){// Array to store the inorder traversal// of the BSTletarr=[];// Call inorder() to do the inorder// on the BST and store it in arrinorder(root,arr);// Now, check if any triplet with given sum// exists in the BST or notfor(leti=0;i<arr.length-2;i++){letl=i+1;letr=arr.length-1;while(l<r){if(arr[l]+arr[r]===sum-arr[i]){returntrue;}elseif(arr[l]+arr[r]<sum-arr[i])l++;elser--;}}returnfalse;}// creating the following BST // 50 // / \ // 30 70 // / \ / \ // 20 40 60 80 letroot=newNode(50);root.left=newNode(30);root.right=newNode(70);root.left.left=newNode(20);root.left.right=newNode(40);root.right.left=newNode(60);root.right.right=newNode(80);letsum=120;if(checkForTriplet(root,sum))console.log("true");elseconsole.log("false");
Output
true
Time Complexity: O(n^2), as we are using nested loops. Auxiliary Space: O(n), where n is the number of nodes in the given bst.