dhyuti-n/xlcost-variable-roles huggingface dataset:
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct snode { Node * l , * r ; } ; Node * newNode ( int data ) { Node * new_node = new Node ; new_node -> data = data ; new_node -> left = new_node -> right = NULL ; return new_node ; } void inorder ( Node * node ) { if ( ! node ) return ; inorder ( node -> left ) ; printf ( " % d ▁ " , node -> data ) ; inorder ( node -> right ) ; } Node * MergeTrees ( Node * t1 , Node * t2 ) { if ( ! t1 ) return t2 ; if ( ! t2 ) return t1 ; stack < snode > s ; snode temp ; temp . l = t1 ; temp . r = t2 ; s . push ( temp ) ; snode n ; while ( ! s . empty ( ) ) { n = s . top ( ) ; s . pop ( ) ; if ( n . l == NULL n . r == NULL ) continue ; n . l -> data += n . r -> data ; if ( n . l -> left == NULL ) n . l -> left = n . r -> left ; else { snode t ; t . l = n . l -> left ; t . r = n . r -> left ; s . push ( t ) ; } if ( n . l -> right == NULL ) n . l -> right = n . r -> right ; else { snode t ; t . l = n . l -> right ; t . r = n . r -> right ; s . push ( t ) ; } } return t1 ; } int main ( ) { Node * root1 = newNode ( 1 ) ; root1 -> left = newNode ( 2 ) ; root1 -> right = newNode ( 3 ) ; root1 -> left -> left = newNode ( 4 ) ; root1 -> left -> right = newNode ( 5 ) ; root1 -> right -> right = newNode ( 6 ) ; Node * root2 = newNode ( 4 ) ; root2 -> left = newNode ( 1 ) ; root2 -> right = newNode ( 7 ) ; root2 -> left -> left = newNode ( 3 ) ; root2 -> right -> left = newNode ( 2 ) ; root2 -> right -> right = newNode ( 6 ) ; Node * root3 = MergeTrees ( root1 , root2 ) ; printf ( " The ▁ Merged ▁ Binary ▁ Tree ▁ is : STRNEWLINE " ) ; inorder ( root3 ) ; return 0 ; }

Accumulator variables: data, stdc
Class/struct names: Node, snode
C++ program:
#include <bits/stdc++.h>
using namespace std ; int findRoot ( pair < int , int > arr [ ] , int n ) { int root = 0 ; for ( int i = 0 ; i < n ; i ++ ) root += ( arr [ i ] . first - arr [ i ] . second ) ; return root ; } int main ( ) { pair < int , int > arr [ ] = { { 1 , 5 } , { 2 , 0 } , { 3 , 0 } , { 4 , 0 } , { 5 , 5 } , { 6 , 5 } } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " % d STRNEWLINE " , findRoot ( arr , n ) ) ; return 0 ; }

Index/key variables: i
Accumulator variables: i, root, stdc
Iterator variables: i
C++ program:
#include <iostream>
using namespace std ; int search ( int arr [ ] , int x , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return -1 ; } void printPostOrder ( int in [ ] , int pre [ ] , int n ) { int root = search ( in , pre [ 0 ] , n ) ; if ( root != 0 ) printPostOrder ( in , pre + 1 , root ) ; if ( root != n - 1 ) printPostOrder ( in + root + 1 , pre + root + 1 , n - root - 1 ) ; cout << pre [ 0 ] << " ▁ " ; } int main ( ) { int in [ ] = { 4 , 2 , 5 , 1 , 3 , 6 } ; int pre [ ] = { 1 , 2 , 4 , 5 , 3 , 6 } ; int n = sizeof ( in ) / sizeof ( in [ 0 ] ) ; cout << " Postorder ▁ traversal ▁ " << endl ; printPostOrder ( in , pre , n ) ; return 0 ; }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
C++ program:
#include <iostream>
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 ; } struct Node * findLCA ( struct Node * root , int n1 , int n2 ) { if ( root == NULL ) return NULL ; if ( root -> key == n1 root -> key == n2 ) return root ; Node * left_lca = findLCA ( root -> left , n1 , n2 ) ; Node * right_lca = findLCA ( root -> right , n1 , n2 ) ; if ( left_lca && right_lca ) return root ; return ( left_lca != NULL ) ? left_lca : right_lca ; } 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 ) ; cout << " LCA ( 4 , ▁ 5 ) ▁ = ▁ " << findLCA ( root , 4 , 5 ) -> key ; cout << " nLCA ( 4 , ▁ 6 ) ▁ = ▁ " << findLCA ( root , 4 , 6 ) -> key ; cout << " nLCA ( 3 , ▁ 4 ) ▁ = ▁ " << findLCA ( root , 3 , 4 ) -> key ; cout << " nLCA ( 2 , ▁ 4 ) ▁ = ▁ " << findLCA ( root , 2 , 4 ) -> key ; return 0 ; }

Class/struct names: Node
C++ program:
#include <iostream>
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 ; } struct Node * findLCAUtil ( struct Node * root , int n1 , int n2 , bool & v1 , bool & v2 ) { if ( root == NULL ) return NULL ; if ( root -> key == n1 ) { v1 = true ; return root ; } if ( root -> key == n2 ) { v2 = true ; return root ; } Node * left_lca = findLCAUtil ( root -> left , n1 , n2 , v1 , v2 ) ; Node * right_lca = findLCAUtil ( root -> right , n1 , n2 , v1 , v2 ) ; if ( left_lca && right_lca ) return root ; return ( left_lca != NULL ) ? left_lca : right_lca ; } bool find ( Node * root , int k ) { if ( root == NULL ) return false ; if ( root -> key == k || find ( root -> left , k ) || find ( root -> right , k ) ) return true ; return false ; } Node * findLCA ( Node * root , int n1 , int n2 ) { bool v1 = false , v2 = false ; Node * lca = findLCAUtil ( root , n1 , n2 , v1 , v2 ) ; if ( v1 && v2 || v1 && find ( lca , n2 ) || v2 && find ( lca , n1 ) ) return lca ; return NULL ; } 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 ) ; Node * lca = findLCA ( root , 4 , 5 ) ; if ( lca != NULL ) cout << " LCA ( 4 , ▁ 5 ) ▁ = ▁ " << lca -> key ; else cout << " Keys ▁ are ▁ not ▁ present ▁ " ; lca = findLCA ( root , 4 , 10 ) ; if ( lca != NULL ) cout << " nLCA ( 4 , ▁ 10 ) ▁ = ▁ " << lca -> key ; else cout << " nKeys ▁ are ▁ not ▁ present ▁ " ; return 0 ; }

Boolean variables: v1, v2
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { Node * left , * right , * parent ; int key ; } ; Node * newNode ( int item ) { Node * temp = new Node ; temp -> key = item ; temp -> parent = 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 ) ; node -> left -> parent = node ; } else if ( key > node -> key ) { node -> right = insert ( node -> right , key ) ; node -> right -> parent = node ; } return node ; } int depth ( Node * node ) { int d = -1 ; while ( node ) { ++ d ; node = node -> parent ; } return d ; } Node * LCA ( Node * n1 , Node * n2 ) { int d1 = depth ( n1 ) , d2 = depth ( n2 ) ; int diff = d1 - d2 ; if ( diff < 0 ) { Node * temp = n1 ; n1 = n2 ; n2 = temp ; diff = - diff ; } while ( diff -- ) n1 = n1 -> parent ; while ( n1 && n2 ) { if ( n1 == n2 ) return n1 ; n1 = n1 -> parent ; n2 = n2 -> parent ; } return NULL ; } int main ( void ) { Node * root = NULL ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 22 ) ; root = insert ( root , 4 ) ; root = insert ( root , 12 ) ; root = insert ( root , 10 ) ; root = insert ( root , 14 ) ; Node * n1 = root -> left -> right -> left ; Node * n2 = root -> right ; Node * lca = LCA ( n1 , n2 ) ; printf ( " LCA ▁ of ▁ % d ▁ and ▁ % d ▁ is ▁ % d ▁ STRNEWLINE " , n1 -> key , n2 -> key , lca -> key ) ; return 0 ; }

Accumulator variables: diff, stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
#define sz ( x )  x.size()
#define pb  push_back
#define left  2 * i + 1
#define right  2 * i + 2
using namespace std ; const int maxn = 100005 ; vector < vector < int > > g ( maxn ) ; int level [ maxn ] ; vector < int > e ; vector < int > l ; int h [ maxn ] ; int st [ 5 * maxn ] ; void add_edge ( int u , int v ) { g [ u ] . pb ( v ) ; g [ v ] . pb ( u ) ; } void leveling ( int src ) { for ( int i = 0 ; i < sz ( g [ src ] ) ; i ++ ) { int des = g [ src ] [ i ] ; if ( ! level [ des ] ) { level [ des ] = level [ src ] + 1 ; leveling ( des ) ; } } } bool visited [ maxn ] ; void dfs ( int src ) { e . pb ( src ) ; visited [ src ] = 1 ; for ( int i = 0 ; i < sz ( g [ src ] ) ; i ++ ) { int des = g [ src ] [ i ] ; if ( ! visited [ des ] ) { dfs ( des ) ; e . pb ( src ) ; } } } void setting_l ( int n ) { for ( int i = 0 ; i < sz ( e ) ; i ++ ) l . pb ( level [ e [ i ] ] ) ; } void setting_h ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) h [ i ] = -1 ; for ( int i = 0 ; i < sz ( e ) ; i ++ ) { if ( h [ e [ i ] ] == -1 ) h [ e [ i ] ] = i ; } } int RMQ ( int ss , int se , int qs , int qe , int i ) { if ( ss > se ) return -1 ; if ( se < qs qe < ss ) return -1 ; if ( qs <= ss && se <= qe ) return st [ i ] ; int mid = ( ss + se ) >> 1 ; int st = RMQ ( ss , mid , qs , qe , left ) ; int en = RMQ ( mid + 1 , se , qs , qe , right ) ; if ( st != -1 && en != -1 ) { if ( l [ st ] < l [ en ] ) return st ; return en ; } else if ( st != -1 ) return st ; else if ( en != -1 ) return en ; } void SegmentTreeConstruction ( int ss , int se , int i ) { if ( ss > se ) return ; if ( ss == se ) { st [ i ] = ss ; return ; } int mid = ( ss + se ) >> 1 ; SegmentTreeConstruction ( ss , mid , left ) ; SegmentTreeConstruction ( mid + 1 , se , right ) ; if ( l [ st [ left ] ] < l [ st [ right ] ] ) st [ i ] = st [ left ] ; else st [ i ] = st [ right ] ; } int LCA ( int x , int y ) { if ( h [ x ] > h [ y ] ) swap ( x , y ) ; return e [ RMQ ( 0 , sz ( l ) - 1 , h [ x ] , h [ y ] , 0 ) ] ; } int main ( ) { ios :: sync_with_stdio ( 0 ) ; int n = 15 , q = 5 ; add_edge ( 1 , 2 ) ; add_edge ( 1 , 3 ) ; add_edge ( 1 , 4 ) ; add_edge ( 3 , 5 ) ; add_edge ( 4 , 6 ) ; add_edge ( 5 , 7 ) ; add_edge ( 5 , 8 ) ; add_edge ( 5 , 9 ) ; add_edge ( 7 , 10 ) ; add_edge ( 7 , 11 ) ; add_edge ( 7 , 12 ) ; add_edge ( 9 , 13 ) ; add_edge ( 9 , 14 ) ; add_edge ( 12 , 15 ) ; level [ 1 ] = 1 ; leveling ( 1 ) ; dfs ( 1 ) ; setting_l ( n ) ; setting_h ( n ) ; SegmentTreeConstruction ( 0 , sz ( l ) - 1 , 0 ) ; cout << LCA ( 10 , 15 ) << endl ; cout << LCA ( 11 , 14 ) << endl ; return 0 ; }

Index/key variables: des, en, i, left, maxn, right, src, st, u, v, x, y
Accumulator variables: i, stdc
Iterator variables: i
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 ; } struct Node * findLCA ( struct Node * root , int n1 , int n2 ) { if ( root == NULL ) return NULL ; if ( root -> key == n1 root -> key == n2 ) return root ; Node * left_lca = findLCA ( root -> left , n1 , n2 ) ; Node * right_lca = findLCA ( root -> right , n1 , n2 ) ; if ( left_lca && right_lca ) return root ; return ( left_lca != NULL ) ? left_lca : right_lca ; } bool printAncestors ( struct Node * root , int target ) { if ( root == NULL ) return false ; if ( root -> key == target ) { cout << root -> key << " ▁ " ; return true ; } if ( printAncestors ( root -> left , target ) || printAncestors ( root -> right , target ) ) { cout << root -> key << " ▁ " ; return true ; } return false ; } bool findCommonNodes ( struct Node * root , int first , int second ) { struct Node * LCA = findLCA ( root , first , second ) ; if ( LCA == NULL ) return false ; printAncestors ( root , LCA -> key ) ; } 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 -> left -> left -> left = newNode ( 8 ) ; root -> right -> left -> left = newNode ( 9 ) ; root -> right -> left -> right = newNode ( 10 ) ; if ( findCommonNodes ( root , 9 , 7 ) == false ) cout << " No ▁ Common ▁ nodes " ; return 0 ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int k ) { Node * temp = new Node ; temp -> key = k ; temp -> left = temp -> right = NULL ; return temp ; } #define V  9			
int euler [ 2 * V - 1 ] ; int level [ 2 * V - 1 ] ; int firstOccurrence [ V + 1 ] ; int ind ; int Log2 ( int x ) { int ans = 0 ; while ( x >>= 1 ) ans ++ ; return ans ; } int RMQUtil ( int index , int ss , int se , int qs , int qe , int * st ) { if ( qs <= ss && qe >= se ) return st [ index ] ; else if ( se < qs ss > qe ) return -1 ; int mid = ( ss + se ) / 2 ; int q1 = RMQUtil ( 2 * index + 1 , ss , mid , qs , qe , st ) ; int q2 = RMQUtil ( 2 * index + 2 , mid + 1 , se , qs , qe , st ) ; if ( q1 == -1 ) return q2 ; else if ( q2 == -1 ) return q1 ; return ( level [ q1 ] < level [ q2 ] ) ? q1 : q2 ; } int RMQ ( int * st , int n , int qs , int qe ) { if ( qs < 0 qe > n -1 qs > qe ) { printf ( " Invalid ▁ Input " ) ; return - 1 ; } return RMQUtil ( 0 , 0 , n - 1 , qs , qe , st ) ; } void constructSTUtil ( int si , int ss , int se , int arr [ ] , int * st ) { if ( ss == se ) st [ si ] = ss ; else { int mid = ( ss + se ) / 2 ; constructSTUtil ( si * 2 + 1 , ss , mid , arr , st ) ; constructSTUtil ( si * 2 + 2 , mid + 1 , se , arr , st ) ; if ( arr [ st [ 2 * si + 1 ] ] < arr [ st [ 2 * si + 2 ] ] ) st [ si ] = st [ 2 * si + 1 ] ; else st [ si ] = st [ 2 * si + 2 ] ; } } int * constructST ( int arr [ ] , int n ) { int x = Log2 ( n ) + 1 ; int max_size = 2 * ( 1 << x ) - 1 ; int * st = new int [ max_size ] ; constructSTUtil ( 0 , 0 , n - 1 , arr , st ) ; return st ; } void eulerTour ( Node * root , int l ) { if ( root ) { euler [ ind ] = root -> key ; level [ ind ] = l ; ind ++ ; if ( firstOccurrence [ root -> key ] == -1 ) firstOccurrence [ root -> key ] = ind - 1 ; if ( root -> left ) { eulerTour ( root -> left , l + 1 ) ; euler [ ind ] = root -> key ; level [ ind ] = l ; ind ++ ; } if ( root -> right ) { eulerTour ( root -> right , l + 1 ) ; euler [ ind ] = root -> key ; level [ ind ] = l ; ind ++ ; } } } int findLCA ( Node * root , int u , int v ) { memset ( firstOccurrence , -1 , sizeof ( int ) * ( V + 1 ) ) ; ind = 0 ; eulerTour ( root , 0 ) ; int * st = constructST ( level , 2 * V - 1 ) ; if ( firstOccurrence [ u ] > firstOccurrence [ v ] ) std :: swap ( u , v ) ; int qs = firstOccurrence [ u ] ; int qe = firstOccurrence [ v ] ; int index = RMQ ( st , 2 * V - 1 , qs , qe ) ; return euler [ index ] ; } 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 -> left -> right -> left = newNode ( 8 ) ; root -> left -> right -> right = newNode ( 9 ) ; int u = 4 , v = 9 ; printf ( " The ▁ LCA ▁ of ▁ node ▁ % d ▁ and ▁ node ▁ % d ▁ is ▁ node ▁ % d . STRNEWLINE " , u , v , findLCA ( root , u , v ) ) ; return 0 ; }

Index/key variables: ind, index, max_size, q1, q2, si, u, v
Accumulator variables: ans, ind, stdc
Class/struct names: Node
C++ program:
#include <iostream>
using namespace std ; int preIndex = 0 ; int search ( int arr [ ] , int startIn , int endIn , int data ) { int i = 0 ; for ( i = startIn ; i < endIn ; i ++ ) { if ( arr [ i ] == data ) { return i ; } } return i ; } void printPost ( int arr [ ] , int pre [ ] , int inStrt , int inEnd ) { if ( inStrt > inEnd ) { return ; } int inIndex = search ( arr , inStrt , inEnd , pre [ preIndex ++ ] ) ; printPost ( arr , pre , inStrt , inIndex - 1 ) ; printPost ( arr , pre , inIndex + 1 , inEnd ) ; cout << arr [ inIndex ] << " ▁ " ; } int main ( ) { int arr [ ] = { 4 , 2 , 5 , 1 , 3 , 6 } ; int pre [ ] = { 1 , 2 , 4 , 5 , 3 , 6 } ; int len = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPost ( arr , pre , 0 , len - 1 ) ; }

Index/key variables: i, inIndex
Accumulator variables: i, preIndex
Iterator variables: i
C++ program:
struct node { int data ; struct node * left ; struct node * right ; } ;

Class/struct names: node
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; struct Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int maxDiffUtil ( Node * t , int * res ) { if ( t == NULL ) return INT_MAX ; if ( t -> left == NULL && t -> right == NULL ) return t -> key ; int val = min ( maxDiffUtil ( t -> left , res ) , maxDiffUtil ( t -> right , res ) ) ; * res = max ( * res , t -> key - val ) ; return min ( val , t -> key ) ; } int maxDiff ( Node * root ) { int res = INT_MIN ; maxDiffUtil ( root , & res ) ; return res ; } void inorder ( Node * root ) { if ( root ) { inorder ( root -> left ) ; printf ( " % d ▁ " , root -> key ) ; inorder ( root -> right ) ; } } int main ( ) { Node * root ; root = newNode ( 8 ) ; root -> left = newNode ( 3 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 4 ) ; root -> left -> right -> right = newNode ( 7 ) ; root -> right = newNode ( 10 ) ; root -> right -> right = newNode ( 14 ) ; root -> right -> right -> left = newNode ( 13 ) ; printf ( " Maximum ▁ difference ▁ between ▁ a ▁ node ▁ and " " ▁ its ▁ ancestor ▁ is ▁ : ▁ % d STRNEWLINE " , maxDiff ( root ) ) ; }

Accumulator variables: stdc
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; bool v1 = false , v2 = false ; struct Node { int data ; Node * left , * right ; } ; struct Node * getNode ( int data ) { struct Node * newNode = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return newNode ; } struct Node * findLCAUtil ( struct Node * root , int n1 , int n2 , bool & v1 , bool & v2 ) { if ( root == NULL ) return NULL ; if ( root -> data == n1 ) { v1 = true ; return root ; } if ( root -> data == n2 ) { v2 = true ; return root ; } Node * left_lca = findLCAUtil ( root -> left , n1 , n2 , v1 , v2 ) ; Node * right_lca = findLCAUtil ( root -> right , n1 , n2 , v1 , v2 ) ; if ( left_lca && right_lca ) return root ; return ( left_lca != NULL ) ? left_lca : right_lca ; } bool find ( Node * root , int k ) { if ( root == NULL ) return false ; if ( root -> data == k || find ( root -> left , k ) || find ( root -> right , k ) ) return true ; return false ; } Node * findLCA ( Node * root , int n1 , int n2 ) { Node * lca = findLCAUtil ( root , n1 , n2 , v1 , v2 ) ; if ( v1 && v2 || v1 && find ( lca , n2 ) || v2 && find ( lca , n1 ) ) return lca ; return NULL ; } bool hasPath ( Node * root , vector < int > & arr , int x ) { if ( ! root ) return false ; arr . push_back ( root -> data ) ; if ( root -> data == x ) return true ; if ( hasPath ( root -> left , arr , x ) || hasPath ( root -> right , arr , x ) ) return true ; arr . pop_back ( ) ; return false ; } void printCommonPath ( Node * root , int n1 , int n2 ) { vector < int > arr ; Node * lca = findLCA ( root , n1 , n2 ) ; if ( lca ) { if ( hasPath ( root , arr , lca -> data ) ) { for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) cout << arr [ i ] << " - > " ; cout << arr [ arr . size ( ) - 1 ] ; } } else cout << " No ▁ Common ▁ Path " ; } int main ( ) { struct Node * root = getNode ( 1 ) ; root -> left = getNode ( 2 ) ; root -> right = getNode ( 3 ) ; root -> left -> left = getNode ( 4 ) ; root -> left -> right = getNode ( 5 ) ; root -> right -> left = getNode ( 6 ) ; root -> right -> right = getNode ( 7 ) ; root -> left -> right -> left = getNode ( 8 ) ; root -> right -> left -> right = getNode ( 9 ) ; int n1 = 4 , n2 = 8 ; printCommonPath ( root , n1 , n2 ) ; return 0 ; }

Index/key variables: i
Accumulator variables: i, stdc
Iterator variables: i
Boolean variables: v1, v2
Class/struct names: Node
C++ program:
#include <bits/stdc++.h>
using namespace std ; void dfs ( vector < int > g [ ] , int u , int parent , int timeIn [ ] , int timeOut [ ] , int & cnt ) { timeIn [ u ] = cnt ++ ; for ( int i = 0 ; i < g [ u ] . size ( ) ; i ++ ) { int v = g [ u ] [ i ] ; if ( v != parent ) dfs ( g , v , u , timeIn , timeOut , cnt ) ; } timeOut [ u ] = cnt ++ ; } void preProcess ( int edges [ ] [ 2 ] , int V , int timeIn [ ] , int timeOut [ ] ) { vector < int > g [ V ] ; for ( int i = 0 ; i < V - 1 ; i ++ ) { int u = edges [ i ] [ 0 ] ; int v = edges [ i ] [ 1 ] ; g [ u ] . push_back ( v ) ; g [ v ] . push_back ( u ) ; } int cnt = 0 ; dfs ( g , 0 , -1 , timeIn , timeOut , cnt ) ; } string isAncestor ( int u , int v , int timeIn [ ] , int timeOut [ ] ) { bool b = ( timeIn [ u ] <= timeIn [ v ] && timeOut [ v ] <= timeOut [ u ] ) ; return ( b ? " yes " : " no " ) ; } int main ( ) { int edges [ ] [ 2 ] = { { 0 , 1 } , { 0 , 2 } , { 1 , 3 } , { 1 , 4 } , { 2 , 5 } , { 4 , 6 } , { 5 , 7 } } ; int E = sizeof ( edges ) / sizeof ( edges [ 0 ] ) ; int V = E + 1 ; int timeIn [ V ] , timeOut [ V ] ; preProcess ( edges , V , timeIn , timeOut ) ; int u = 1 ; int v = 6 ; cout << isAncestor ( u , v , timeIn , timeOut ) << endl ; u = 1 ; v = 7 ; cout << isAncestor ( u , v , timeIn , timeOut ) << endl ; return 0 ; }

Index/key variables: V, i, u, v
Accumulator variables: cnt, i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; struct Node { int data ; Node * left , * right ; } ; struct Node * getNode ( int data ) { struct Node * newNode = new Node ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return newNode ; } bool hasPath ( Node * root , vector < int > & arr , int x ) { if ( ! root ) return false ; arr . push_back ( root -> data ) ; if ( root -> data == x ) return true ; if ( hasPath ( root -> left , arr , x ) || hasPath ( root -> right , arr , x ) ) return true ; arr . pop_back ( ) ; return false ; } void printPath ( Node * root , int x ) { vector < int > arr ; if ( hasPath ( root , arr , x ) ) { for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) cout << arr [ i ] << " - > " ; cout << arr [ arr . size ( ) - 1 ] ; } else cout << " No ▁ Path " ; } int main ( ) { struct Node * root = getNode ( 1 ) ; root -> left = getNode ( 2 ) ; root -> right = getNode ( 3 ) ; root -> left -> left = getNode ( 4 ) ; root -> left -> right = getNode ( 5 ) ; root -> right -> left = getNode ( 6 ) ; root -> right -> right = getNode ( 7 ) ; int x = 5 ; printPath ( root , x ) ; return 0 ; }

Index/key variables: i
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 * left ; struct node * right ; } ; bool printAncestors ( struct node * root , int target ) { if ( root == NULL ) return false ; if ( root -> data == target ) return true ; if ( printAncestors ( root -> left , target ) || printAncestors ( root -> right , target ) ) { cout << root -> data << " ▁ " ; return true ; } return false ; } struct node * newnode ( int data ) { struct node * node = ( struct node * ) malloc ( sizeof ( struct node ) ) ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } int main ( ) { struct node * root = newnode ( 1 ) ; root -> left = newnode ( 2 ) ; root -> right = newnode ( 3 ) ; root -> left -> left = newnode ( 4 ) ; root -> left -> right = newnode ( 5 ) ; root -> left -> left -> left = newnode ( 7 ) ; printAncestors ( root , 7 ) ; getchar ( ) ; return 0 ; }

Accumulator variables: stdc
Class/struct names: node