dhyuti-n/xlcost-variable-roles huggingface dataset:
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; Node * next ; } ; void printList ( Node * node ) { while ( node != NULL ) { printf ( " % d ▁ " , node -> data ) ; node = node -> next ; } } Node * mergeKLists ( Node * arr [ ] , int last ) { for ( int i = 1 ; i <= last ; i ++ ) { while ( true ) { Node * head_0 = arr [ 0 ] , * head_i = arr [ i ] ; if ( head_i == NULL ) break ; if ( head_0 -> data >= head_i -> data ) { arr [ i ] = head_i -> next ; head_i -> next = head_0 ; arr [ 0 ] = head_i ; } else while ( head_0 -> next != NULL ) { if ( head_0 -> next -> data >= head_i -> data ) { arr [ i ] = head_i -> next ; head_i -> next = head_0 -> next ; head_0 -> next = head_i ; break ; } head_0 = head_0 -> next ; if ( head_0 -> next == NULL ) { arr [ i ] = head_i -> next ; head_i -> next = NULL ; head_0 -> next = head_i ; head_0 -> next -> next = NULL ; break ; } } } } return arr [ 0 ] ; } Node * newNode ( int data ) { struct Node * temp = new Node ; temp -> data = data ; temp -> next = NULL ; return temp ; } int main ( ) { int k = 3 ; int n = 4 ; Node * arr [ k ] ; arr [ 0 ] = newNode ( 1 ) ; arr [ 0 ] -> next = newNode ( 3 ) ; arr [ 0 ] -> next -> next = newNode ( 5 ) ; arr [ 0 ] -> next -> next -> next = newNode ( 7 ) ; arr [ 1 ] = newNode ( 2 ) ; arr [ 1 ] -> next = newNode ( 4 ) ; arr [ 1 ] -> next -> next = newNode ( 6 ) ; arr [ 1 ] -> next -> next -> next = newNode ( 8 ) ; arr [ 2 ] = newNode ( 0 ) ; arr [ 2 ] -> next = newNode ( 9 ) ; arr [ 2 ] -> next -> next = newNode ( 10 ) ; arr [ 2 ] -> next -> next -> next = newNode ( 11 ) ; Node * head = mergeKLists ( arr , k - 1 ) ; printList ( head ) ; return 0 ; }

Index/key variables: i, k
Accumulator variables: i, stdc
Iterator variables: i
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * next ; } ; Node * newNode ( int key ) { struct Node * temp = new Node ; temp -> data = key ; temp -> next = NULL ; return temp ; } void printList ( Node * node ) { while ( node != NULL ) { printf ( " % d ▁ " , node -> data ) ; node = node -> next ; } } Node * merge ( Node * h1 , Node * h2 ) { if ( ! h1 ) return h2 ; if ( ! h2 ) return h1 ; if ( h1 -> data < h2 -> data ) { h1 -> next = merge ( h1 -> next , h2 ) ; return h1 ; } else { h2 -> next = merge ( h1 , h2 -> next ) ; return h2 ; } } int main ( ) { Node * head1 = newNode ( 1 ) ; head1 -> next = newNode ( 3 ) ; head1 -> next -> next = newNode ( 5 ) ; Node * head2 = newNode ( 0 ) ; head2 -> next = newNode ( 2 ) ; head2 -> next -> next = newNode ( 4 ) ; Node * mergedhead = merge ( head1 , head2 ) ; printList ( mergedhead ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * next ; } ; struct Node * newNode ( int key ) { struct Node * temp = new Node ; temp -> data = key ; temp -> next = NULL ; return temp ; } void printList ( struct Node * node ) { while ( node != NULL ) { printf ( " % d ▁ " , node -> data ) ; node = node -> next ; } } struct Node * mergeUtil ( struct Node * h1 , struct Node * h2 ) { if ( ! h1 -> next ) { h1 -> next = h2 ; return h1 ; } struct Node * curr1 = h1 , * next1 = h1 -> next ; struct Node * curr2 = h2 , * next2 = h2 -> next ; while ( next1 && curr2 ) { if ( ( curr2 -> data ) >= ( curr1 -> data ) && ( curr2 -> data ) <= ( next1 -> data ) ) { next2 = curr2 -> next ; curr1 -> next = curr2 ; curr2 -> next = next1 ; curr1 = curr2 ; curr2 = next2 ; } else { if ( next1 -> next ) { next1 = next1 -> next ; curr1 = curr1 -> next ; } else { next1 -> next = curr2 ; return h1 ; } } } return h1 ; } struct Node * merge ( struct Node * h1 , struct Node * h2 ) { if ( ! h1 ) return h2 ; if ( ! h2 ) return h1 ; if ( h1 -> data < h2 -> data ) return mergeUtil ( h1 , h2 ) ; else return mergeUtil ( h2 , h1 ) ; } int main ( ) { struct Node * head1 = newNode ( 1 ) ; head1 -> next = newNode ( 3 ) ; head1 -> next -> next = newNode ( 5 ) ; struct Node * head2 = newNode ( 0 ) ; head2 -> next = newNode ( 2 ) ; head2 -> next -> next = newNode ( 4 ) ; struct Node * mergedhead = merge ( head1 , head2 ) ; printList ( mergedhead ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * next ; } ; void swapNodes ( struct Node * * head_ref , struct Node * currX , struct Node * currY , struct Node * prevY ) { * head_ref = currY ; prevY -> next = currX ; struct Node * temp = currY -> next ; currY -> next = currX -> next ; currX -> next = temp ; } struct Node * recurSelectionSort ( struct Node * head ) { if ( head -> next == NULL ) return head ; struct Node * min = head ; struct Node * beforeMin = NULL ; struct Node * ptr ; for ( ptr = head ; ptr -> next != NULL ; ptr = ptr -> next ) { if ( ptr -> next -> data < min -> data ) { min = ptr -> next ; beforeMin = ptr ; } } if ( min != head ) swapNodes ( & head , head , min , beforeMin ) ; head -> next = recurSelectionSort ( head -> next ) ; return head ; } void sort ( struct Node * * head_ref ) { if ( ( * head_ref ) == NULL ) return ; * head_ref = recurSelectionSort ( * head_ref ) ; } void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( struct Node * head ) { while ( head != NULL ) { cout << head -> data << " ▁ " ; head = head -> next ; } } int main ( ) { struct Node * head = NULL ; push ( & head , 6 ) ; push ( & head , 4 ) ; push ( & head , 8 ) ; push ( & head , 12 ) ; push ( & head , 10 ) ; cout << " Linked ▁ list ▁ before ▁ sorting : n " ; printList ( head ) ; sort ( & head ) ; cout << " Linked list after sorting : n " ; printList ( head ) ; return 0 ; }

Accumulator variables: stdc
Iterator variables: ptr
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; Node * next ; } ; Node * getNode ( int data ) { Node * newNode = ( Node * ) malloc ( sizeof ( Node ) ) ; newNode -> data = data ; newNode -> next = NULL ; return newNode ; } void insertAfterNthNode ( Node * head , int n , int x ) { if ( head == NULL ) return ; Node * newNode = getNode ( x ) ; Node * ptr = head ; int len = 0 , i ; while ( ptr != NULL ) { len ++ ; ptr = ptr -> next ; } ptr = head ; for ( i = 1 ; i <= ( len - n ) ; i ++ ) ptr = ptr -> next ; newNode -> next = ptr -> next ; ptr -> next = newNode ; } void printList ( Node * head ) { while ( head != NULL ) { cout << head -> data << " ▁ " ; head = head -> next ; } } int main ( ) { Node * head = getNode ( 1 ) ; head -> next = getNode ( 3 ) ; head -> next -> next = getNode ( 4 ) ; head -> next -> next -> next = getNode ( 5 ) ; int n = 4 , x = 2 ; cout << " Original ▁ Linked ▁ List : ▁ " ; printList ( head ) ; insertAfterNthNode ( head , n , x ) ; cout << " Linked List After Insertion : " printList ( head ) ; return 0 ; }

Accumulator variables: i, len, stdc
Iterator variables: i
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; Node * next ; } ; Node * getNode ( int data ) { Node * newNode = ( Node * ) malloc ( sizeof ( Node ) ) ; newNode -> data = data ; newNode -> next = NULL ; return newNode ; } void insertAfterNthNode ( Node * head , int n , int x ) { if ( head == NULL ) return ; Node * newNode = getNode ( x ) ; Node * slow_ptr = head ; Node * fast_ptr = head ; for ( int i = 1 ; i <= n - 1 ; i ++ ) fast_ptr = fast_ptr -> next ; while ( fast_ptr -> next != NULL ) { slow_ptr = slow_ptr -> next ; fast_ptr = fast_ptr -> next ; } newNode -> next = slow_ptr -> next ; slow_ptr -> next = newNode ; } void printList ( Node * head ) { while ( head != NULL ) { cout << head -> data << " ▁ " ; head = head -> next ; } } int main ( ) { Node * head = getNode ( 1 ) ; head -> next = getNode ( 3 ) ; head -> next -> next = getNode ( 4 ) ; head -> next -> next -> next = getNode ( 5 ) ; int n = 4 , x = 2 ; cout << " Original ▁ Linked ▁ List : ▁ " ; printList ( head ) ; insertAfterNthNode ( head , n , x ) ; cout << " Linked List After Insertion : " printList ( head ) ; return 0 ; }

Accumulator variables: i, stdc
Iterator variables: i
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; class Node { public : int data ; Node * next ; } ; void setMiddleHead ( Node * * head ) { if ( * head == NULL ) return ; Node * one_node = ( * head ) ; Node * two_node = ( * head ) ; Node * prev = NULL ; while ( two_node != NULL && two_node -> next != NULL ) { prev = one_node ; two_node = two_node -> next -> next ; one_node = one_node -> next ; } prev -> next = prev -> next -> next ; one_node -> next = ( * head ) ; ( * head ) = one_node ; } void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( Node * ptr ) { while ( ptr != NULL ) { cout << ptr -> data << " ▁ " ; ptr = ptr -> next ; } cout << endl ; } int main ( ) { Node * head = NULL ; int i ; for ( i = 5 ; i > 0 ; i -- ) push ( & head , i ) ; cout << " ▁ list ▁ before : ▁ " ; printList ( head ) ; setMiddleHead ( & head ) ; cout << " ▁ list ▁ After : ▁ " ; printList ( head ) ; return 0 ; }

Accumulator variables: i, stdc
Iterator variables: i
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; Node * next , * prev ; } ; void insert ( Node * * head_ref , int data ) { Node * new_node = new Node ; new_node -> data = data ; if ( * head_ref == NULL ) { new_node -> next = new_node ; new_node -> prev = new_node ; } else { Node * last = ( * head_ref ) -> prev ; new_node -> next = * head_ref ; new_node -> prev = last ; last -> next = ( * head_ref ) -> prev = new_node ; } * head_ref = new_node ; } Node * merge ( Node * first , Node * second ) { if ( ! first ) return second ; if ( ! second ) return first ; if ( first -> data < second -> data ) { first -> next = merge ( first -> next , second ) ; first -> next -> prev = first ; first -> prev = NULL ; return first ; } else { second -> next = merge ( first , second -> next ) ; second -> next -> prev = second ; second -> prev = NULL ; return second ; } } Node * mergeUtil ( Node * head1 , Node * head2 ) { if ( ! head1 ) return head2 ; if ( ! head2 ) return head1 ; Node * last_node ; if ( head1 -> prev -> data < head2 -> prev -> data ) last_node = head2 -> prev ; else last_node = head1 -> prev ; head1 -> prev -> next = head2 -> prev -> next = NULL ; Node * finalHead = merge ( head1 , head2 ) ; finalHead -> prev = last_node ; last_node -> next = finalHead ; return finalHead ; } void printList ( Node * head ) { Node * temp = head ; while ( temp -> next != head ) { cout << temp -> data << " ▁ " ; temp = temp -> next ; } cout << temp -> data << " ▁ " ; } int main ( ) { Node * head1 = NULL , * head2 = NULL ; insert ( & head1 , 8 ) ; insert ( & head1 , 5 ) ; insert ( & head1 , 3 ) ; insert ( & head1 , 1 ) ; insert ( & head2 , 11 ) ; insert ( & head2 , 9 ) ; insert ( & head2 , 7 ) ; insert ( & head2 , 2 ) ; Node * newHead = mergeUtil ( head1 , head2 ) ; cout << " Final ▁ Sorted ▁ List : ▁ " ; printList ( newHead ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * prev , * next ; } ; Node * newNode ( int val ) { Node * temp = new Node ; temp -> data = val ; temp -> prev = temp -> next = nullptr ; return temp ; } void printList ( Node * head ) { while ( head -> next != nullptr ) { cout << head -> data << " ▁ < - - > ▁ " ; head = head -> next ; } cout << head -> data << endl ; } void insert ( Node * * head , int val ) { Node * temp = newNode ( val ) ; temp -> next = * head ; ( * head ) -> prev = temp ; ( * head ) = temp ; } void reverseList ( Node * * head ) { Node * left = * head , * right = * head ; while ( right -> next != nullptr ) right = right -> next ; while ( left != right && left -> prev != right ) { swap ( left -> data , right -> data ) ; left = left -> next ; right = right -> prev ; } } int main ( ) { Node * head = newNode ( 5 ) ; insert ( & head , 4 ) ; insert ( & head , 3 ) ; insert ( & head , 2 ) ; insert ( & head , 1 ) ; printList ( head ) ; cout << " List ▁ After ▁ Reversing " << endl ; reverseList ( & head ) ; printList ( head ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { char data ; struct Node * next ; struct Node * prev ; } ; void push ( struct Node * * head_ref , char new_data ) { struct Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; new_node -> prev = NULL ; if ( ( * head_ref ) != NULL ) ( * head_ref ) -> prev = new_node ; ( * head_ref ) = new_node ; } bool isPalindrome ( struct Node * left ) { if ( left == NULL ) return true ; struct Node * right = left ; while ( right -> next != NULL ) right = right -> next ; while ( left != right ) { if ( left -> data != right -> data ) return false ; left = left -> next ; right = right -> prev ; } return true ; } int main ( ) { struct Node * head = NULL ; push ( & head , ' l ' ) ; push ( & head , ' e ' ) ; push ( & head , ' v ' ) ; push ( & head , ' e ' ) ; push ( & head , ' l ' ) ; if ( isPalindrome ( head ) ) printf ( " It ▁ is ▁ Palindrome " ) ; else printf ( " Not ▁ Palindrome " ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
#include <cinttypes>
using namespace std ; class Node { public : int data ; Node * npx ; } ; Node * XOR ( Node * a , Node * b ) { return reinterpret_cast < Node * > ( reinterpret_cast < uintptr_t > ( a ) ^ reinterpret_cast < uintptr_t > ( b ) ) ; } void insert ( Node * * head_ref , int data ) { Node * new_node = new Node ( ) ; new_node -> data = data ; new_node -> npx = * head_ref ; if ( * head_ref != NULL ) { ( * head_ref ) -> npx = XOR ( new_node , ( * head_ref ) -> npx ) ; } * head_ref = new_node ; } void printList ( Node * head ) { Node * curr = head ; Node * prev = NULL ; Node * next ; cout << " Following ▁ are ▁ the ▁ nodes ▁ of ▁ Linked ▁ List : ▁ STRNEWLINE " ; while ( curr != NULL ) { cout << curr -> data << " ▁ " ; next = XOR ( prev , curr -> npx ) ; prev = curr ; curr = next ; } } int main ( ) { Node * head = NULL ; insert ( & head , 10 ) ; insert ( & head , 20 ) ; insert ( & head , 30 ) ; insert ( & head , 40 ) ; printList ( head ) ; return ( 0 ) ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; void printLevels ( Node * root , int low , int high ) { queue < Node * > Q ; Node * marker = new Node ; int level = 1 ; Q . push ( root ) ; Q . push ( marker ) ; while ( Q . empty ( ) == false ) { Node * n = Q . front ( ) ; Q . pop ( ) ; if ( n == marker ) { cout << endl ; level ++ ; if ( Q . empty ( ) == true level > high ) break ; Q . push ( marker ) ; continue ; } if ( level >= low ) cout << n -> key << " ▁ " ; if ( n -> left != NULL ) Q . push ( n -> left ) ; if ( n -> right != NULL ) Q . push ( n -> right ) ; } } Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int main ( ) { struct Node * root = newNode ( 20 ) ; root -> left = newNode ( 8 ) ; root -> right = newNode ( 22 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 12 ) ; root -> left -> right -> left = newNode ( 10 ) ; root -> left -> right -> right = newNode ( 14 ) ; cout << " Level ▁ Order ▁ traversal ▁ between ▁ given ▁ two ▁ levels ▁ is " ; printLevels ( root , 2 , 3 ) ; return 0 ; }

Accumulator variables: level, stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; class node { public : int data ; node * left ; node * right ; node ( int data ) { this -> data = data ; this -> left = NULL ; this -> right = NULL ; } } ; void printKDistant ( node * root , int k ) { if ( root == NULL k < 0 ) return ; if ( k == 0 ) { cout << root -> data << " ▁ " ; return ; } printKDistant ( root -> left , k - 1 ) ; printKDistant ( root -> right , k - 1 ) ; } int main ( ) { node * root = new node ( 1 ) ; root -> left = new node ( 2 ) ; root -> right = new node ( 3 ) ; root -> left -> left = new node ( 4 ) ; root -> left -> right = new node ( 5 ) ; root -> right -> left = new node ( 8 ) ; printKDistant ( root , 2 ) ; 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 ; } ; Node * newNode ( int data ) { Node * newnode = new Node ( ) ; newnode -> data = data ; newnode -> left = newnode -> right = NULL ; } bool printKDistant ( Node * root , int klevel ) { queue < Node * > q ; int level = 1 ; bool flag = false ; q . push ( root ) ; q . push ( NULL ) ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) ; if ( level == klevel && temp != NULL ) { flag = true ; cout << temp -> data << " ▁ " ; } q . pop ( ) ; if ( temp == NULL ) { if ( q . front ( ) ) q . push ( NULL ) ; level += 1 ; if ( level > klevel ) break ; } else { if ( temp -> left ) q . push ( temp -> left ) ; if ( temp -> right ) q . push ( temp -> right ) ; } } cout << endl ; return flag ; } int main ( ) { Node * root = newNode ( 20 ) ; root -> left = newNode ( 10 ) ; root -> right = newNode ( 30 ) ; root -> left -> left = newNode ( 5 ) ; root -> left -> right = newNode ( 15 ) ; root -> left -> right -> left = newNode ( 12 ) ; root -> right -> left = newNode ( 25 ) ; root -> right -> right = newNode ( 40 ) ; cout << " data ▁ at ▁ level ▁ 1 ▁ : ▁ " ; int ret = printKDistant ( root , 1 ) ; if ( ret == false ) cout << " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels STRNEWLINE " ; cout << " data ▁ at ▁ level ▁ 2 ▁ : ▁ " ; ret = printKDistant ( root , 2 ) ; if ( ret == false ) cout << " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels STRNEWLINE " ; cout << " data ▁ at ▁ level ▁ 3 ▁ : ▁ " ; ret = printKDistant ( root , 3 ) ; if ( ret == false ) cout << " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels STRNEWLINE " ; cout << " data ▁ at ▁ level ▁ 6 ▁ : ▁ " ; ret = printKDistant ( root , 6 ) ; if ( ret == false ) cout << " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels STRNEWLINE " ; return 0 ; }

Accumulator variables: level, stdc
Boolean variables: flag
Class/struct names: Node
C++ program:
#include <iostream>
using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; void printLeafNodes ( Node * root ) { if ( ! root ) return ; if ( ! root -> left && ! root -> right ) { cout << root -> data << " ▁ " ; return ; } if ( root -> left ) printLeafNodes ( root -> left ) ; if ( root -> right ) printLeafNodes ( root -> right ) ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> left -> left = newNode ( 6 ) ; root -> right -> left -> right = newNode ( 7 ) ; root -> right -> right -> left = newNode ( 9 ) ; root -> right -> right -> right = newNode ( 10 ) ; printLeafNodes ( root ) ; return 0 ; }

Class/struct names: Node
C++ program:
#include <iostream>
using namespace std ; struct node { int data ; struct node * left , * right ; } ; void printkdistanceNodeDown ( node * root , int k ) { if ( root == NULL k < 0 ) return ; if ( k == 0 ) { cout << root -> data << endl ; return ; } printkdistanceNodeDown ( root -> left , k - 1 ) ; printkdistanceNodeDown ( root -> right , k - 1 ) ; } int printkdistanceNode ( node * root , node * target , int k ) { if ( root == NULL ) return -1 ; if ( root == target ) { printkdistanceNodeDown ( root , k ) ; return 0 ; } int dl = printkdistanceNode ( root -> left , target , k ) ; if ( dl != -1 ) { if ( dl + 1 == k ) cout << root -> data << endl ; else printkdistanceNodeDown ( root -> right , k - dl - 2 ) ; return 1 + dl ; } int dr = printkdistanceNode ( root -> right , target , k ) ; if ( dr != -1 ) { if ( dr + 1 == k ) cout << root -> data << endl ; else printkdistanceNodeDown ( root -> left , k - dr - 2 ) ; return 1 + dr ; } return -1 ; } node * newnode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { node * root = newnode ( 20 ) ; root -> left = newnode ( 8 ) ; root -> right = newnode ( 22 ) ; root -> left -> left = newnode ( 4 ) ; root -> left -> right = newnode ( 12 ) ; root -> left -> right -> left = newnode ( 10 ) ; root -> left -> right -> right = newnode ( 14 ) ; node * target = root -> left -> right ; printkdistanceNode ( root , target , 2 ) ; return 0 ; }

Class/struct names: node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct node { struct node * left , * right ; int key ; } ; node * newNode ( int key ) { node * temp = new node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return temp ; } void printSingles ( struct node * root ) { if ( root == NULL ) return ; if ( root -> left != NULL && root -> right != NULL ) { printSingles ( root -> left ) ; printSingles ( root -> right ) ; } else if ( root -> right != NULL ) { cout << root -> right -> key << " ▁ " ; printSingles ( root -> right ) ; } else if ( root -> left != NULL ) { cout << root -> left -> key << " ▁ " ; printSingles ( root -> left ) ; } } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> right = newNode ( 4 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> left -> left = newNode ( 6 ) ; printSingles ( root ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: node
C++ program:
#include <iostream>
using namespace std ; #define MAX_HEIGHT  10000
struct Node { int key ; Node * left , * right ; } ; Node * newNode ( int key ) { Node * node = new Node ; node -> key = key ; node -> left = node -> right = NULL ; return ( node ) ; } void kDistantFromLeafUtil ( Node * node , int path [ ] , bool visited [ ] , int pathLen , int k ) { if ( node == NULL ) return ; path [ pathLen ] = node -> key ; visited [ pathLen ] = false ; pathLen ++ ; if ( node -> left == NULL && node -> right == NULL && pathLen - k - 1 >= 0 && visited [ pathLen - k - 1 ] == false ) { cout << path [ pathLen - k - 1 ] << " ▁ " ; visited [ pathLen - k - 1 ] = true ; return ; } kDistantFromLeafUtil ( node -> left , path , visited , pathLen , k ) ; kDistantFromLeafUtil ( node -> right , path , visited , pathLen , k ) ; } void printKDistantfromLeaf ( Node * node , int k ) { int path [ MAX_HEIGHT ] ; bool visited [ MAX_HEIGHT ] = { false } ; kDistantFromLeafUtil ( node , path , visited , 0 , k ) ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> right -> left -> right = newNode ( 8 ) ; cout << " Nodes ▁ at ▁ distance ▁ 2 ▁ are : ▁ " ; printKDistantfromLeaf ( root , 2 ) ; return 0 ; }

Index/key variables: MAX_HEIGHT, pathLen
Accumulator variables: pathLen
Class/struct names: Node