dhyuti-n/xlcost-variable-roles huggingface dataset:
C++ program:
#include <bits/stdc++.h>
using namespace std ; class TreeNode { public : int val ; TreeNode * left ; TreeNode * right ; TreeNode ( int x ) { val = x ; } } ; set < TreeNode * > s ; stack < TreeNode * > st ; TreeNode * buildTree ( int preorder [ ] , int inorder [ ] , int n ) { TreeNode * root = NULL ; for ( int pre = 0 , in = 0 ; pre < n ; ) { TreeNode * node = NULL ; do { node = new TreeNode ( preorder [ pre ] ) ; if ( root == NULL ) { root = node ; } if ( st . size ( ) > 0 ) { if ( s . find ( st . top ( ) ) != s . end ( ) ) { s . erase ( st . top ( ) ) ; st . top ( ) -> right = node ; st . pop ( ) ; } else { st . top ( ) -> left = node ; } } st . push ( node ) ; } while ( preorder [ pre ++ ] != inorder [ in ] && pre < n ) ; node = NULL ; while ( st . size ( ) > 0 && in < n && st . top ( ) -> val == inorder [ in ] ) { node = st . top ( ) ; st . pop ( ) ; in ++ ; } if ( node != NULL ) { s . insert ( node ) ; st . push ( node ) ; } } return root ; } void printInorder ( TreeNode * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; cout << node -> val << " ▁ " ; printInorder ( node -> right ) ; } int main ( ) { int in [ ] = { 9 , 8 , 4 , 2 , 10 , 5 , 10 , 1 , 6 , 3 , 13 , 12 , 7 } ; int pre [ ] = { 1 , 2 , 4 , 8 , 9 , 5 , 10 , 10 , 3 , 6 , 7 , 12 , 13 } ; int len = sizeof ( in ) / sizeof ( int ) ; TreeNode * root = buildTree ( pre , in , len ) ; printInorder ( root ) ; return 0 ; }

Index/key variables: in, pre
Accumulator variables: in, pre, stdc
Iterator variables: pre
Class/struct names: TreeNode
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; Node * newNode ( int val ) { Node * temp = new Node ; temp -> data = val ; temp -> left = temp -> right = NULL ; return temp ; } void insertToHash ( Node * root , unordered_set < int > & s ) { if ( ! root ) return ; insertToHash ( root -> left , s ) ; s . insert ( root -> data ) ; insertToHash ( root -> right , s ) ; } bool checkBSTs ( Node * root1 , Node * root2 ) { if ( ! root1 && ! root2 ) return true ; if ( ( root1 && ! root2 ) || ( ! root1 && root2 ) ) return false ; unordered_set < int > s1 , s2 ; insertToHash ( root1 , s1 ) ; insertToHash ( root2 , s2 ) ; return ( s1 == s2 ) ; } int main ( ) { Node * root1 = newNode ( 15 ) ; root1 -> left = newNode ( 10 ) ; root1 -> right = newNode ( 20 ) ; root1 -> left -> left = newNode ( 5 ) ; root1 -> left -> right = newNode ( 12 ) ; root1 -> right -> right = newNode ( 25 ) ; Node * root2 = newNode ( 15 ) ; root2 -> left = newNode ( 12 ) ; root2 -> right = newNode ( 20 ) ; root2 -> left -> left = newNode ( 5 ) ; root2 -> left -> left -> right = newNode ( 10 ) ; root2 -> right -> right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; Node * newNode ( int val ) { Node * temp = new Node ; temp -> data = val ; temp -> left = temp -> right = NULL ; return temp ; } void storeInorder ( Node * root , vector < int > & v ) { if ( ! root ) return ; storeInorder ( root -> left , v ) ; v . push_back ( root -> data ) ; storeInorder ( root -> right , v ) ; } bool checkBSTs ( Node * root1 , Node * root2 ) { if ( ! root1 && ! root2 ) return true ; if ( ( root1 && ! root2 ) || ( ! root1 && root2 ) ) return false ; vector < int > v1 , v2 ; storeInorder ( root1 , v1 ) ; storeInorder ( root2 , v2 ) ; return ( v1 == v2 ) ; } int main ( ) { Node * root1 = newNode ( 15 ) ; root1 -> left = newNode ( 10 ) ; root1 -> right = newNode ( 20 ) ; root1 -> left -> left = newNode ( 5 ) ; root1 -> left -> right = newNode ( 12 ) ; root1 -> right -> right = newNode ( 25 ) ; Node * root2 = newNode ( 15 ) ; root2 -> left = newNode ( 12 ) ; root2 -> right = newNode ( 20 ) ; root2 -> left -> left = newNode ( 5 ) ; root2 -> left -> left -> right = newNode ( 10 ) ; root2 -> right -> right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { struct Node * left , * right ; int key ; } ; struct Node * newNode ( int key ) { struct Node * ptr = new Node ; ptr -> key = key ; ptr -> left = ptr -> right = NULL ; return ptr ; } struct Node * insert ( struct Node * root , int key ) { if ( ! root ) root = newNode ( key ) ; else if ( root -> key > key ) root -> left = insert ( root -> left , key ) ; else if ( root -> key < key ) root -> right = insert ( root -> right , key ) ; return root ; } int distanceFromRoot ( struct Node * root , int x ) { if ( root -> key == x ) return 0 ; else if ( root -> key > x ) return 1 + distanceFromRoot ( root -> left , x ) ; return 1 + distanceFromRoot ( root -> right , x ) ; } int distanceBetween2 ( struct Node * root , int a , int b ) { if ( ! root ) return 0 ; if ( root -> key > a && root -> key > b ) return distanceBetween2 ( root -> left , a , b ) ; if ( root -> key < a && root -> key < b ) return distanceBetween2 ( root -> right , a , b ) ; if ( root -> key >= a && root -> key <= b ) return distanceFromRoot ( root , a ) + distanceFromRoot ( root , b ) ; } int findDistWrapper ( Node * root , int a , int b ) { if ( a > b ) swap ( a , b ) ; return distanceBetween2 ( root , a , b ) ; } int main ( ) { struct Node * root = NULL ; root = insert ( root , 20 ) ; insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 30 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; int a = 5 , b = 55 ; cout << findDistWrapper ( root , 5 , 35 ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct node { int data ; struct node * left , * right ; } ; void RangeTraversal ( node * root , int n1 , int n2 ) { if ( ! root ) return ; node * curr = root ; while ( curr ) { if ( curr -> left == NULL ) { if ( curr -> data <= n2 && curr -> data >= n1 ) { cout << curr -> data << " ▁ " ; } curr = curr -> right ; } else { node * pre = curr -> left ; while ( pre -> right != NULL && pre -> right != curr ) pre = pre -> right ; if ( pre -> right == NULL ) { pre -> right = curr ; curr = curr -> left ; } else { pre -> right = NULL ; if ( curr -> data <= n2 && curr -> data >= n1 ) { cout << curr -> data << " ▁ " ; } curr = curr -> right ; } } } } node * newNode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> right = temp -> left = NULL ; return temp ; } int main ( ) { node * root = newNode ( 4 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 7 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 10 ) ; RangeTraversal ( root , 4 , 12 ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct node { int data ; struct node * left , * right ; } ; bool inRange ( node * root , int low , int high ) { return root -> data >= low && root -> data <= high ; } bool getCountUtil ( node * root , int low , int high , int * count ) { if ( root == NULL ) return true ; bool l = getCountUtil ( root -> left , low , high , count ) ; bool r = getCountUtil ( root -> right , low , high , count ) ; if ( l && r && inRange ( root , low , high ) ) { ++ * count ; return true ; } return false ; } int getCount ( node * root , int low , int high ) { int count = 0 ; getCountUtil ( root , low , high , & count ) ; return count ; } node * newNode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int main ( ) { node * root = newNode ( 10 ) ; root -> left = newNode ( 5 ) ; root -> right = newNode ( 50 ) ; root -> left -> left = newNode ( 1 ) ; root -> right -> left = newNode ( 40 ) ; root -> right -> right = newNode ( 100 ) ; int l = 5 ; int h = 45 ; cout << " Count ▁ of ▁ subtrees ▁ in ▁ [ " << l << " , ▁ " << h << " ] ▁ is ▁ " << getCount ( root , l , h ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; struct Node * newNode ( int data ) { struct Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } struct Node * insert ( struct Node * root , int data ) { if ( root == NULL ) return newNode ( data ) ; if ( data < root -> data ) root -> left = insert ( root -> left , data ) ; else if ( data > root -> data ) root -> right = insert ( root -> right , data ) ; return root ; } void inorder ( struct Node * root ) { if ( root != NULL ) { inorder ( root -> left ) ; cout << root -> data << " ▁ " ; inorder ( root -> right ) ; } } struct Node * leafDelete ( struct Node * root ) { if ( root == NULL ) return NULL ; if ( root -> left == NULL && root -> right == NULL ) { free ( root ) ; return NULL ; } root -> left = leafDelete ( root -> left ) ; root -> right = leafDelete ( root -> right ) ; return root ; } int main ( ) { struct Node * root = NULL ; root = insert ( root , 20 ) ; insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 30 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; cout << " Inorder ▁ before ▁ Deleting ▁ the ▁ leaf ▁ Node . " << endl ; inorder ( root ) ; cout << endl ; leafDelete ( root ) ; cout << " INorder ▁ after ▁ Deleting ▁ the ▁ leaf ▁ Node . " << endl ; inorder ( root ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; Node * left , * right ; } ; struct Node * createNode ( int data ) { Node * new_Node = new Node ; new_Node -> left = NULL ; new_Node -> right = NULL ; new_Node -> data = data ; return new_Node ; } struct Node * insert ( Node * root , int key ) { if ( root == NULL ) return createNode ( key ) ; if ( root -> data > key ) root -> left = insert ( root -> left , key ) ; else if ( root -> data < key ) root -> right = insert ( root -> right , key ) ; return root ; } int ksmallestElementSumRec ( Node * root , int k , int & count ) { if ( root == NULL ) return 0 ; if ( count > k ) return 0 ; int res = ksmallestElementSumRec ( root -> left , k , count ) ; if ( count >= k ) return res ; res += root -> data ; count ++ ; if ( count >= k ) return res ; return res + ksmallestElementSumRec ( root -> right , k , count ) ; } int ksmallestElementSum ( struct Node * root , int k ) { int count = 0 ; ksmallestElementSumRec ( root , k , count ) ; } int main ( ) { Node * root = NULL ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 4 ) ; root = insert ( root , 12 ) ; root = insert ( root , 10 ) ; root = insert ( root , 14 ) ; root = insert ( root , 22 ) ; int k = 3 ; cout << ksmallestElementSum ( root , k ) << endl ; return 0 ; }

Accumulator variables: count, res, stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; int lCount ; int Sum ; Node * left ; Node * right ; } ; struct Node * createNode ( int data ) { Node * new_Node = new Node ; new_Node -> left = NULL ; new_Node -> right = NULL ; new_Node -> data = data ; new_Node -> lCount = 0 ; new_Node -> Sum = 0 ; return new_Node ; } struct Node * insert ( Node * root , int key ) { if ( root == NULL ) return createNode ( key ) ; if ( root -> data > key ) { root -> lCount ++ ; root -> Sum += key ; root -> left = insert ( root -> left , key ) ; } else if ( root -> data < key ) root -> right = insert ( root -> right , key ) ; return root ; } void ksmallestElementSumRec ( Node * root , int k , int & temp_sum ) { if ( root == NULL ) return ; if ( ( root -> lCount + 1 ) == k ) { temp_sum += root -> data + root -> Sum ; return ; } else if ( k > root -> lCount ) { temp_sum += root -> data + root -> Sum ; k = k - ( root -> lCount + 1 ) ; ksmallestElementSumRec ( root -> right , k , temp_sum ) ; } else ksmallestElementSumRec ( root -> left , k , temp_sum ) ; } int ksmallestElementSum ( struct Node * root , int k ) { int sum = 0 ; ksmallestElementSumRec ( root , k , sum ) ; return sum ; } int main ( ) { Node * root = NULL ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 4 ) ; root = insert ( root , 12 ) ; root = insert ( root , 10 ) ; root = insert ( root , 14 ) ; root = insert ( root , 22 ) ; int k = 3 ; cout << ksmallestElementSum ( root , k ) << endl ; return 0 ; }

Accumulator variables: Sum, lCount, stdc, temp_sum
Class/struct names: Node
C++ program:
struct Node { int key ; struct Node * left , * right ; } ;

Class/struct names: Node
C++ program:
#include <iostream>
#include <stdlib.h>
using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * getnode ( int info ) { Node * p = ( Node * ) malloc ( sizeof ( Node ) ) ; p -> data = info ; p -> right = NULL ; p -> left = NULL ; return p ; } void find_p_s ( Node * root , int a , Node * * p , Node * * q ) { if ( ! root ) return ; find_p_s ( root -> left , a , p , q ) ; if ( root && root -> data > a ) { if ( ( ! * q ) || ( * q ) && ( * q ) -> data > root -> data ) * q = root ; } else if ( root && root -> data < a ) { * p = root ; } find_p_s ( root -> right , a , p , q ) ; } int main ( ) { Node * root1 = getnode ( 50 ) ; root1 -> left = getnode ( 20 ) ; root1 -> right = getnode ( 60 ) ; root1 -> left -> left = getnode ( 10 ) ; root1 -> left -> right = getnode ( 30 ) ; root1 -> right -> left = getnode ( 55 ) ; root1 -> right -> right = getnode ( 70 ) ; Node * p = NULL , * q = NULL ; find_p_s ( root1 , 55 , & p , & q ) ; if ( p ) cout << p -> data ; if ( q ) cout << " ▁ " << q -> data ; return 0 ; }

Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; void findPreSuc ( Node * root , Node * & pre , Node * & suc , int key ) { if ( root == NULL ) return ; while ( root != NULL ) { if ( root -> key == key ) { if ( root -> right ) { suc = root -> right ; while ( suc -> left ) suc = suc -> left ; } if ( root -> left ) { pre = root -> left ; while ( pre -> right ) pre = pre -> right ; } return ; } else if ( root -> key < key ) { pre = root ; root = root -> right ; } else { suc = root ; root = root -> left ; } } } Node * newNode ( int item ) { Node * temp = new Node ; temp -> key = item ; temp -> left = temp -> right = NULL ; return temp ; } Node * insert ( Node * node , int key ) { if ( node == NULL ) return newNode ( key ) ; if ( key < node -> key ) node -> left = insert ( node -> left , key ) ; else node -> right = insert ( node -> right , key ) ; return node ; } int main ( ) { int key = 65 ; Node * root = NULL ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; Node * pre = NULL , * suc = NULL ; findPreSuc ( root , pre , suc , key ) ; if ( pre != NULL ) cout << " Predecessor ▁ is ▁ " << pre -> key << endl ; else cout << " - 1" ; if ( suc != NULL ) cout << " Successor ▁ is ▁ " << suc -> key ; else cout << " - 1" ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; class node { public : int key ; node * left ; node * right ; } ; void convertBSTtoDLL ( node * root , node * * head , node * * tail ) { if ( root == NULL ) return ; if ( root -> left ) convertBSTtoDLL ( root -> left , head , tail ) ; root -> left = * tail ; if ( * tail ) ( * tail ) -> right = root ; else * head = root ; * tail = root ; if ( root -> right ) convertBSTtoDLL ( root -> right , head , tail ) ; } bool isPresentInDLL ( node * head , node * tail , int sum ) { while ( head != tail ) { int curr = head -> key + tail -> key ; if ( curr == sum ) return true ; else if ( curr > sum ) tail = tail -> left ; else head = head -> right ; } return false ; } bool isTripletPresent ( node * root ) { if ( root == NULL ) return false ; node * head = NULL ; node * tail = NULL ; convertBSTtoDLL ( root , & head , & tail ) ; while ( ( head -> right != tail ) && ( head -> key < 0 ) ) { if ( isPresentInDLL ( head -> right , tail , -1 * head -> key ) ) return true ; else head = head -> right ; } return false ; } node * newNode ( int num ) { node * temp = new node ( ) ; temp -> key = num ; temp -> left = temp -> right = NULL ; return temp ; } node * insert ( node * root , int key ) { if ( root == NULL ) return newNode ( key ) ; if ( root -> key > key ) root -> left = insert ( root -> left , key ) ; else root -> right = insert ( root -> right , key ) ; return root ; } int main ( ) { node * root = NULL ; root = insert ( root , 6 ) ; root = insert ( root , -13 ) ; root = insert ( root , 14 ) ; root = insert ( root , -8 ) ; root = insert ( root , 15 ) ; root = insert ( root , 13 ) ; root = insert ( root , 7 ) ; if ( isTripletPresent ( root ) ) cout << " Present " ; else cout << " Not ▁ Present " ; return 0 ; }

Accumulator variables: stdc
Class/struct names: node
C++ program:
#include <bits/stdc++.h>
using namespace std ; #define MAX_SIZE  100
class node { public : int val ; node * left , * right ; } ; class Stack { public : int size ; int top ; node * * array ; } ; Stack * createStack ( int size ) { Stack * stack = new Stack ( ) ; stack -> size = size ; stack -> top = -1 ; stack -> array = new node * [ ( stack -> size * sizeof ( node * ) ) ] ; return stack ; } int isFull ( Stack * stack ) { return stack -> top - 1 == stack -> size ; } int isEmpty ( Stack * stack ) { return stack -> top == -1 ; } void push ( Stack * stack , node * node ) { if ( isFull ( stack ) ) return ; stack -> array [ ++ stack -> top ] = node ; } node * pop ( Stack * stack ) { if ( isEmpty ( stack ) ) return NULL ; return stack -> array [ stack -> top -- ] ; } bool isPairPresent ( node * root , int target ) { Stack * s1 = createStack ( MAX_SIZE ) ; Stack * s2 = createStack ( MAX_SIZE ) ; bool done1 = false , done2 = false ; int val1 = 0 , val2 = 0 ; node * curr1 = root , * curr2 = root ; while ( 1 ) { while ( done1 == false ) { if ( curr1 != NULL ) { push ( s1 , curr1 ) ; curr1 = curr1 -> left ; } else { if ( isEmpty ( s1 ) ) done1 = 1 ; else { curr1 = pop ( s1 ) ; val1 = curr1 -> val ; curr1 = curr1 -> right ; done1 = 1 ; } } } while ( done2 == false ) { if ( curr2 != NULL ) { push ( s2 , curr2 ) ; curr2 = curr2 -> right ; } else { if ( isEmpty ( s2 ) ) done2 = 1 ; else { curr2 = pop ( s2 ) ; val2 = curr2 -> val ; curr2 = curr2 -> left ; done2 = 1 ; } } } if ( ( val1 != val2 ) && ( val1 + val2 ) == target ) { cout << " Pair ▁ Found : ▁ " << val1 << " + ▁ " << val2 << " ▁ = ▁ " << target << endl ; return true ; } else if ( ( val1 + val2 ) < target ) done1 = false ; else if ( ( val1 + val2 ) > target ) done2 = false ; if ( val1 >= val2 ) return false ; } } node * NewNode ( int val ) { node * tmp = new node ( ) ; tmp -> val = val ; tmp -> right = tmp -> left = NULL ; return tmp ; } int main ( ) { node * root = NewNode ( 15 ) ; root -> left = NewNode ( 10 ) ; root -> right = NewNode ( 20 ) ; root -> left -> left = NewNode ( 8 ) ; root -> left -> right = NewNode ( 12 ) ; root -> right -> left = NewNode ( 16 ) ; root -> right -> right = NewNode ( 25 ) ; int target = 33 ; if ( isPairPresent ( root , target ) == false ) cout << " No such values are found " ; return 0 ; }

Accumulator variables: stdc, top
Boolean variables: done1, done2
Class/struct names: Stack, node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * NewNode ( int data ) { Node * temp = ( Node * ) malloc ( sizeof ( Node ) ) ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } Node * insert ( Node * root , int key ) { if ( root == NULL ) return NewNode ( key ) ; if ( key < root -> data ) root -> left = insert ( root -> left , key ) ; else root -> right = insert ( root -> right , key ) ; return root ; } bool findpairUtil ( Node * root , int sum , unordered_set < int > & set ) { if ( root == NULL ) return false ; if ( findpairUtil ( root -> left , sum , set ) ) return true ; if ( set . find ( sum - root -> data ) != set . end ( ) ) { cout << " Pair ▁ is ▁ found ▁ ( " << sum - root -> data << " , ▁ " << root -> data << " ) " << endl ; return true ; } else set . insert ( root -> data ) ; return findpairUtil ( root -> right , sum , set ) ; } void findPair ( Node * root , int sum ) { unordered_set < int > set ; if ( ! findpairUtil ( root , sum , set ) ) cout << " Pairs ▁ do ▁ not ▁ exit " << endl ; } int main ( ) { Node * root = NULL ; root = insert ( root , 15 ) ; root = insert ( root , 10 ) ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 12 ) ; root = insert ( root , 16 ) ; root = insert ( root , 25 ) ; root = insert ( root , 10 ) ; int sum = 33 ; findPair ( root , sum ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int num ) { struct Node * temp = new Node ; temp -> data = num ; temp -> left = temp -> right = NULL ; return temp ; } Node * insert ( Node * root , int key ) { if ( root == NULL ) return newNode ( key ) ; if ( root -> data > key ) root -> left = insert ( root -> left , key ) ; else root -> right = insert ( root -> right , key ) ; return root ; } void storeInorder ( Node * ptr , vector < int > & vect ) { if ( ptr == NULL ) return ; storeInorder ( ptr -> left , vect ) ; vect . push_back ( ptr -> data ) ; storeInorder ( ptr -> right , vect ) ; } void pairSumUtil ( vector < int > & vect1 , vector < int > & vect2 , int sum ) { int left = 0 ; int right = vect2 . size ( ) - 1 ; while ( left < vect1 . size ( ) && right >= 0 ) { if ( vect1 [ left ] + vect2 [ right ] == sum ) { cout << " ( " << vect1 [ left ] << " , ▁ " << vect2 [ right ] << " ) , ▁ " ; left ++ ; right -- ; } else if ( vect1 [ left ] + vect2 [ right ] < sum ) left ++ ; else right -- ; } } void pairSum ( Node * root1 , Node * root2 , int sum ) { vector < int > vect1 , vect2 ; storeInorder ( root1 , vect1 ) ; storeInorder ( root2 , vect2 ) ; pairSumUtil ( vect1 , vect2 , sum ) ; } int main ( ) { struct Node * root1 = NULL ; root1 = insert ( root1 , 8 ) ; root1 = insert ( root1 , 10 ) ; root1 = insert ( root1 , 3 ) ; root1 = insert ( root1 , 6 ) ; root1 = insert ( root1 , 1 ) ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 7 ) ; root1 = insert ( root1 , 14 ) ; root1 = insert ( root1 , 13 ) ; struct Node * root2 = NULL ; root2 = insert ( root2 , 5 ) ; root2 = insert ( root2 , 18 ) ; root2 = insert ( root2 , 2 ) ; root2 = insert ( root2 , 1 ) ; root2 = insert ( root2 , 3 ) ; root2 = insert ( root2 , 4 ) ; int sum = 10 ; pairSum ( root1 , root2 , sum ) ; return 0 ; }

Index/key variables: left, right
Accumulator variables: left, right, stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = ( Node * ) malloc ( sizeof ( Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } Node * insertLevelOrder ( int arr [ ] , Node * root , int i , int n ) { if ( i < n ) { Node * temp = newNode ( arr [ i ] ) ; root = temp ; root -> left = insertLevelOrder ( arr , root -> left , 2 * i + 1 , n ) ; root -> right = insertLevelOrder ( arr , root -> right , 2 * i + 2 , n ) ; } return root ; } void inOrder ( Node * root ) { if ( root != NULL ) { inOrder ( root -> left ) ; cout << root -> data << " ▁ " ; inOrder ( root -> right ) ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 6 , 6 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Node * root = insertLevelOrder ( arr , root , 0 , n ) ; inOrder ( root ) ; }

Index/key variables: i
Accumulator variables: stdc
Class/struct names: Node