codeparrot/xlcost-text-to-code huggingface dataset:
Delete every Kth node from circular linked list | C ++ program to delete every kth Node from circular linked list . ; structure for a Node ; Utility function to print the circular linked list ; Function to delete every kth Node ; If list is empty , simply return . ; take two pointers - current and previous ; Check if Node is the only Node \ If yes , we reached the goal , therefore return . ; Print intermediate list . ; If more than one Node present in the list , Make previous pointer point to current Iterate current pointer k times , i . e . current Node is to be deleted . ; If Node to be deleted is head ; If Node to be deleted is last Node . ; Function to insert a Node at the end of a Circular linked list ; Create a new Node ; if the list is empty , make the new Node head Also , it will point to itself . ; traverse the list to reach the last Node and insert the Node ; Driver program to test above functions ; insert Nodes in the circular linked list ; Delete every kth Node from the circular linked list .
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; Node ( int x ) { data = x ; next = NULL ; } } ; void printList ( Node * head ) { if ( head == NULL ) return ; Node * temp = head ; do { cout << temp -> data << " - > " ; temp = temp -> next ; } while ( temp != head ) ; cout << head -> data << endl ; } void deleteK ( Node * * head_ref , int k ) { Node * head = * head_ref ; if ( head == NULL ) return ; Node * curr = head , * prev ; while ( true ) { if ( curr -> next == head && curr == head ) break ; printList ( head ) ; for ( int i = 0 ; i < k ; i ++ ) { prev = curr ; curr = curr -> next ; } if ( curr == head ) { prev = head ; while ( prev -> next != head ) prev = prev -> next ; head = curr -> next ; prev -> next = head ; * head_ref = head ; free ( curr ) ; } else if ( curr -> next == head ) { prev -> next = head ; free ( curr ) ; } else { prev -> next = curr -> next ; free ( curr ) ; } } } void insertNode ( Node * * head_ref , int x ) { Node * head = * head_ref ; Node * temp = new Node ( x ) ; if ( head == NULL ) { temp -> next = temp ; * head_ref = temp ; } else { Node * temp1 = head ; while ( temp1 -> next != head ) temp1 = temp1 -> next ; temp1 -> next = temp ; temp -> next = head ; } } int main ( ) { struct Node * head = NULL ; insertNode ( & head , 1 ) ; insertNode ( & head , 2 ) ; insertNode ( & head , 3 ) ; insertNode ( & head , 4 ) ; insertNode ( & head , 5 ) ; insertNode ( & head , 6 ) ; insertNode ( & head , 7 ) ; insertNode ( & head , 8 ) ; insertNode ( & head , 9 ) ; int k = 4 ; deleteK ( & head , k ) ; return 0 ; }
Convert an Array to a Circular Doubly Linked List | CPP program to convert array to circular doubly linked list ; Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to convert array into list ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Driver Code ; Array to be converted ; Start Pointer ; Create the List ; Display the list
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * next ; struct node * prev ; } ; struct node * getNode ( ) { return ( ( struct node * ) malloc ( sizeof ( struct node ) ) ) ; } int displayList ( struct node * temp ) { struct node * t = temp ; if ( temp == NULL ) return 0 ; else { cout << " The ▁ list ▁ is : ▁ " ; while ( temp -> next != t ) { cout << temp -> data << " ▁ " ; temp = temp -> next ; } cout << temp -> data ; return 1 ; } } void createList ( int arr [ ] , int n , struct node * * start ) { struct node * newNode , * temp ; int i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode -> data = arr [ i ] ; if ( i == 0 ) { * start = newNode ; newNode -> prev = * start ; newNode -> next = * start ; } else { temp = ( * start ) -> prev ; temp -> next = newNode ; newNode -> next = * start ; newNode -> prev = temp ; temp = * start ; temp -> prev = newNode ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; struct node * start = NULL ; createList ( arr , n , & start ) ; displayList ( start ) ; return 0 ; }
Lucky alive person in a circle | Code Solution to sword puzzle | C ++ code to find the luckiest person ; Node structure ; Function to find the luckiest person ; Create a single node circular linked list . ; Starting from first soldier . ; condition for evaluating the existence of single soldier who is not killed . ; deleting soldier from the circular list who is killed in the fight . ; Returning the Luckiest soldier who remains alive . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> next = NULL ; return node ; } int alivesol ( int Num ) { if ( Num == 1 ) return 1 ; Node * last = newNode ( 1 ) ; last -> next = last ; for ( int i = 2 ; i <= Num ; i ++ ) { Node * temp = newNode ( i ) ; temp -> next = last -> next ; last -> next = temp ; last = temp ; } Node * curr = last -> next ; Node * temp ; while ( curr -> next != curr ) { temp = curr ; curr = curr -> next ; temp -> next = curr -> next ; delete curr ; temp = temp -> next ; curr = temp ; } int res = temp -> data ; delete temp ; return res ; } int main ( ) { int N = 100 ; cout << alivesol ( N ) << endl ; return 0 ; }
Lowest Common Ancestor of the deepest leaves of a Binary Tree | C ++ program for the above approach ; Node of a Binary Tree ; Function to create a new tree Node ; Function to find the depth of the Binary Tree ; If root is not null ; Left recursive subtree ; Right recursive subtree ; Returns the maximum depth ; Function to perform the depth first search on the binary tree ; If root is null ; If curr is equal to depth ; Left recursive subtree ; Right recursive subtree ; If left and right are not null ; Return left , if left is not null Otherwise return right ; Function to find the LCA of the deepest nodes of the binary tree ; If root is null ; Stores the deepest depth of the binary tree ; Return the LCA of the nodes at level depth ; Driver Code ; Given Binary Tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { struct Node * left ; struct Node * right ; int data ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> data = key ; temp -> left = temp -> right = NULL ; return temp ; } int finddepth ( Node * root ) { if ( ! root ) return 0 ; int left = finddepth ( root -> left ) ; int right = finddepth ( root -> right ) ; return 1 + max ( left , right ) ; } Node * dfs ( Node * root , int curr , int depth ) { if ( ! root ) return NULL ; if ( curr == depth ) return root ; Node * left = dfs ( root -> left , curr + 1 , depth ) ; Node * right = dfs ( root -> right , curr + 1 , depth ) ; if ( left != NULL && right != NULL ) return root ; return left ? left : right ; } Node * lcaOfDeepestLeaves ( Node * root ) { if ( ! root ) return NULL ; int depth = finddepth ( root ) - 1 ; return dfs ( root , 0 , depth ) ; } 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 -> left = newNode ( 8 ) ; root -> right -> left -> right = newNode ( 9 ) ; cout << lcaOfDeepestLeaves ( root ) -> data ; return 0 ; }
Check if two nodes are on same path in a tree | Set 2 | C ++ program to check if two nodes are on same path in a tree without using any extra space ; Function to filter the return Values ; Utility function to check if nodes are on same path or not ; Condition to check if any vertex is equal to given two vertex or not ; Check if the current position has 1 ; Recursive call ; Return LCA ; Function to check if nodes lies on same path or not ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int filter ( int x , int y , int z ) { if ( x != -1 && y != -1 ) { return z ; } return x == -1 ? y : x ; } int samePathUtil ( int mtrx [ ] [ 7 ] , int vrtx , int v1 , int v2 , int i ) { int ans = -1 ; if ( i == v1 i == v2 ) return i ; for ( int j = 0 ; j < vrtx ; j ++ ) { if ( mtrx [ i ] [ j ] == 1 ) { ans = filter ( ans , samePathUtil ( mtrx , vrtx , v1 , v2 , j ) , i ) ; } } return ans ; } bool isVertexAtSamePath ( int mtrx [ ] [ 7 ] , int vrtx , int v1 , int v2 , int i ) { int lca = samePathUtil ( mtrx , vrtx , v1 - 1 , v2 - 1 , i ) ; if ( lca == v1 - 1 lca == v2 - 1 ) return true ; return false ; } int main ( ) { int vrtx = 7 , edge = 6 ; int mtrx [ 7 ] [ 7 ] = { { 0 , 1 , 1 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } } ; int v1 = 1 , v2 = 5 ; if ( isVertexAtSamePath ( mtrx , vrtx , v1 , v2 , 0 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find distance between two nodes in the given Binary tree for Q queries | C ++ Program to find distance between two nodes using LCA ; log2 ( MAX ) ; Array to store the level of each node ; Vector to store tree ; Pre - Processing to calculate values of lca [ ] [ ] , dist [ ] [ ] ; Using recursion formula to calculate the values of lca [ ] [ ] ; Storing the level of each node ; Using recursion formula to calculate the values of lca [ ] [ ] and dist [ ] [ ] ; Function to find the distance between given nodes u and v ; The node which is present farthest from the root node is taken as v . If u is farther from root node then swap the two ; Finding the ancestor of v which is at same level as u ; Adding distance of node v till its 2 ^ i - th ancestor ; If u is the ancestor of v then u is the LCA of u and v ; Finding the node closest to the root which is not the common ancestor of u and v i . e . a node x such that x is not the common ancestor of u and v but lca [ x ] [ 0 ] is ; Adding the distance of v and u to its 2 ^ i - th ancestor ; Adding the distance of u and v to its first ancestor ; Driver Code ; Number of nodes ; Add edges with their cost ; Initialising lca and dist values with - 1 and 0 respectively ; Perform DFS ; Query 1 : { 1 , 3 } ; Query 2 : { 2 , 3 } ; Query 3 : { 3 , 5 }
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000 NEW_LINE #define log  10  NEW_LINE int level [ MAX ] ; int lca [ MAX ] [ log ] ; int dist [ MAX ] [ log ] ; vector < pair < int , int > > graph [ MAX ] ; void addEdge ( int u , int v , int cost ) { graph [ u ] . push_back ( { v , cost } ) ; graph [ v ] . push_back ( { u , cost } ) ; } void dfs ( int node , int parent , int h , int cost ) { lca [ node ] [ 0 ] = parent ; level [ node ] = h ; if ( parent != -1 ) { dist [ node ] [ 0 ] = cost ; } for ( int i = 1 ; i < log ; i ++ ) { if ( lca [ node ] [ i - 1 ] != -1 ) { lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; dist [ node ] [ i ] = dist [ node ] [ i - 1 ] + dist [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; } } for ( auto i : graph [ node ] ) { if ( i . first == parent ) continue ; dfs ( i . first , node , h + 1 , i . second ) ; } } void findDistance ( int u , int v ) { int ans = 0 ; if ( level [ u ] > level [ v ] ) swap ( u , v ) ; for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != -1 && level [ lca [ v ] [ i ] ] >= level [ u ] ) { ans += dist [ v ] [ i ] ; v = lca [ v ] [ i ] ; } } if ( v == u ) { cout << ans << endl ; } else { for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != lca [ u ] [ i ] ) { ans += dist [ u ] [ i ] + dist [ v ] [ i ] ; v = lca [ v ] [ i ] ; u = lca [ u ] [ i ] ; } } ans += dist [ u ] [ 0 ] + dist [ v ] [ 0 ] ; cout << ans << endl ; } } int main ( ) { int n = 5 ; addEdge ( 1 , 2 , 2 ) ; addEdge ( 1 , 3 , 3 ) ; addEdge ( 2 , 4 , 5 ) ; addEdge ( 2 , 5 , 7 ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j < log ; j ++ ) { lca [ i ] [ j ] = -1 ; dist [ i ] [ j ] = 0 ; } } dfs ( 1 , -1 , 0 , 0 ) ; findDistance ( 1 , 3 ) ; findDistance ( 2 , 3 ) ; findDistance ( 3 , 5 ) ; return 0 ; }
Count of all prime weight nodes between given nodes in the given Tree | C ++ program Count prime weight nodes between two nodes in the given tree ; Function to perform Sieve Of Eratosthenes for prime number ; Initialize all entries of prime it as true A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Check if prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to perform dfs ; Stores parent of each node ; Stores level of each node from root ; Function to perform prime number between the path ; The node which is present farthest from the root node is taken as v If u is farther from root node then swap the two ; Find the ancestor of v which is at same level as u ; If Weight is prime increment count ; If u is the ancestor of v then u is the LCA of u and v Now check if weigh [ v ] is prime or not ; When v and u are on the same level but are in different subtree . Now move both u and v up by 1 till they are not same ; If weight of first ancestor is prime ; Driver code ; Precompute all the prime numbers till MAX ; Weights of the node ; Edges of the tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000 NEW_LINE int weight [ MAX ] ; int level [ MAX ] ; int par [ MAX ] ; bool prime [ MAX + 1 ] ; vector < int > graph [ MAX ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= MAX ; i += p ) prime [ i ] = false ; } } } void dfs ( int node , int parent , int h ) { par [ node ] = parent ; level [ node ] = h ; for ( int child : graph [ node ] ) { if ( child == parent ) continue ; dfs ( child , node , h + 1 ) ; } } int findPrimeOnPath ( int u , int v ) { int count = 0 ; if ( level [ u ] > level [ v ] ) swap ( u , v ) ; int d = level [ v ] - level [ u ] ; while ( d -- ) { if ( prime [ weight [ v ] ] ) count ++ ; v = par [ v ] ; } if ( v == u ) { if ( prime [ weight [ v ] ] ) count ++ ; return count ; } while ( v != u ) { if ( prime [ weight [ v ] ] ) count ++ ; if ( prime [ weight [ u ] ] ) count ++ ; u = par [ u ] ; v = par [ v ] ; } if ( prime [ weight [ v ] ] ) count ++ ; return count ; } int main ( ) { SieveOfEratosthenes ( ) ; weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . push_back ( 2 ) ; graph [ 2 ] . push_back ( 3 ) ; graph [ 2 ] . push_back ( 4 ) ; graph [ 1 ] . push_back ( 5 ) ; dfs ( 1 , -1 , 0 ) ; int u = 3 , v = 5 ; cout << findPrimeOnPath ( u , v ) << endl ; return 0 ; }
Lowest Common Ancestor for a Set of Nodes in a Rooted Tree | C ++ Program to find the LCA in a rooted tree for a given set of nodes ; Set time 1 initially ; Case for root node ; In - time for node ; Out - time for the node ; level [ i ] -- > Level of node i ; t_in [ i ] -- > In - time of node i ; t_out [ i ] -- > Out - time of node i ; Fill the level , in - time and out - time of all nodes ; To find minimum in - time among all nodes in LCA set ; To find maximum in - time among all nodes in LCA set ; Node with same minimum and maximum out time is LCA for the set ; Take the minimum level as level of LCA ; If i - th node is at a higher level than that of the minimum among the nodes of the given set ; Compare in - time , out - time and level of i - th node to the respective extremes among all nodes of the given set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int T = 1 ; void dfs ( int node , int parent , vector < int > g [ ] , int level [ ] , int t_in [ ] , int t_out [ ] ) { if ( parent == -1 ) { level [ node ] = 1 ; } else { level [ node ] = level [ parent ] + 1 ; } t_in [ node ] = T ; for ( auto i : g [ node ] ) { if ( i != parent ) { T ++ ; dfs ( i , node , g , level , t_in , t_out ) ; } } T ++ ; t_out [ node ] = T ; } int findLCA ( int n , vector < int > g [ ] , vector < int > v ) { int level [ n + 1 ] ; int t_in [ n + 1 ] ; int t_out [ n + 1 ] ; dfs ( 1 , -1 , g , level , t_in , t_out ) ; int mint = INT_MAX , maxt = INT_MIN ; int minv = -1 , maxv = -1 ; for ( auto i = v . begin ( ) ; i != v . end ( ) ; i ++ ) { if ( t_in [ * i ] < mint ) { mint = t_in [ * i ] ; minv = * i ; } if ( t_out [ * i ] > maxt ) { maxt = t_out [ * i ] ; maxv = * i ; } } if ( minv == maxv ) { return minv ; } int lev = min ( level [ minv ] , level [ maxv ] ) ; int node , l = INT_MIN ; for ( int i = 1 ; i <= n ; i ++ ) { if ( level [ i ] > lev ) continue ; if ( t_in [ i ] <= mint && t_out [ i ] >= maxt && level [ i ] > l ) { node = i ; l = level [ i ] ; } } return node ; } int main ( ) { int n = 10 ; vector < int > g [ n + 1 ] ; g [ 1 ] . push_back ( 2 ) ; g [ 2 ] . push_back ( 1 ) ; g [ 1 ] . push_back ( 3 ) ; g [ 3 ] . push_back ( 1 ) ; g [ 1 ] . push_back ( 4 ) ; g [ 4 ] . push_back ( 1 ) ; g [ 2 ] . push_back ( 5 ) ; g [ 5 ] . push_back ( 2 ) ; g [ 2 ] . push_back ( 6 ) ; g [ 6 ] . push_back ( 2 ) ; g [ 3 ] . push_back ( 7 ) ; g [ 7 ] . push_back ( 3 ) ; g [ 4 ] . push_back ( 10 ) ; g [ 10 ] . push_back ( 4 ) ; g [ 8 ] . push_back ( 7 ) ; g [ 7 ] . push_back ( 8 ) ; g [ 9 ] . push_back ( 7 ) ; g [ 7 ] . push_back ( 9 ) ; vector < int > v = { 7 , 3 , 8 } ; cout << findLCA ( n , g , v ) << endl ; }
Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree | C ++ implementation of the approach ; Structure of binary tree ; Function to create a new node ; Function to store the path from root node to given node of the tree in path vector and then returns true if the path exists otherwise false ; Function to print the minimum and the maximum value present in the path connecting the given two nodes of the given binary tree ; To store the path from the root node to a ; To store the path from the root node to b ; To store the minimum and the maximum value in the path from LCA to a ; To store the minimum and the maximum value in the path from LCA to b ; If both a and b are present in the tree ; Compare the paths to get the first different value ; Find minimum and maximum value in the path from LCA to a ; Find minimum and maximum value in the path from LCA to b ; Minimum of min values in first path and second path ; Maximum of max values in first path and second path ; If no path exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { Node * left ; Node * right ; int data ; } ; Node * newNode ( int key ) { Node * node = new Node ( ) ; node -> left = node -> right = NULL ; node -> data = key ; return node ; } bool FindPath ( Node * root , vector < int > & path , int key ) { if ( root == NULL ) return false ; path . push_back ( root -> data ) ; if ( root -> data == key ) return true ; if ( FindPath ( root -> left , path , key ) || FindPath ( root -> right , path , key ) ) return true ; path . pop_back ( ) ; return false ; } int minMaxNodeInPath ( Node * root , int a , int b ) { vector < int > Path1 ; vector < int > Path2 ; int min1 = INT_MAX ; int max1 = INT_MIN ; int min2 = INT_MAX ; int max2 = INT_MIN ; int i = 0 ; int j = 0 ; if ( FindPath ( root , Path1 , a ) && FindPath ( root , Path2 , b ) ) { for ( i = 0 ; i < Path1 . size ( ) && Path2 . size ( ) ; i ++ ) if ( Path1 [ i ] != Path2 [ i ] ) break ; i -- ; j = i ; for ( ; i < Path1 . size ( ) ; i ++ ) { if ( min1 > Path1 [ i ] ) min1 = Path1 [ i ] ; if ( max1 < Path1 [ i ] ) max1 = Path1 [ i ] ; } for ( ; j < Path2 . size ( ) ; j ++ ) { if ( min2 > Path2 [ j ] ) min2 = Path2 [ j ] ; if ( max2 < Path2 [ j ] ) max2 = Path2 [ j ] ; } cout << " Min ▁ = ▁ " << min ( min1 , min2 ) << endl ; cout << " Max ▁ = ▁ " << max ( max1 , max2 ) ; } else cout << " Max = -1 " ; } int main ( ) { Node * root = newNode ( 20 ) ; root -> left = newNode ( 8 ) ; root -> right = newNode ( 22 ) ; root -> left -> left = newNode ( 5 ) ; root -> left -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 4 ) ; root -> right -> right = newNode ( 25 ) ; root -> left -> right -> left = newNode ( 10 ) ; root -> left -> right -> right = newNode ( 14 ) ; int a = 5 ; int b = 1454 ; minMaxNodeInPath ( root , a , b ) ; return 0 ; }
Sum of all odd nodes in the path connecting two given nodes | C ++ program to find sum of all odd nodes in the path connecting two given nodes ; Binary Tree node ; Utitlity function to create a new Binary Tree node ; Function to check if there is a path from root to the given node . It also populates ' arr ' with the given path ; if root is NULL there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr ' ; if it is the required node return true ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , remove current node ' s ▁ value ▁ from ▁ ▁ ' arr 'and then return false ; Function to get the sum of odd nodes in the path between any two nodes in a binary tree ; vector to store the path of first node n1 from root ; vector to store the path of second node n2 from root ; Get intersection point ; Keep moving forward until no intersection is found ; calculate sum of ODD nodes from the path ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return node ; } bool getPath ( Node * root , vector < int > & arr , int x ) { if ( ! root ) return false ; arr . push_back ( root -> data ) ; if ( root -> data == x ) return true ; if ( getPath ( root -> left , arr , x ) || getPath ( root -> right , arr , x ) ) return true ; arr . pop_back ( ) ; return false ; } int sumOddNodes ( Node * root , int n1 , int n2 ) { vector < int > path1 ; vector < int > path2 ; getPath ( root , path1 , n1 ) ; getPath ( root , path2 , n2 ) ; int intersection = -1 ; int i = 0 , j = 0 ; while ( i != path1 . size ( ) || j != path2 . size ( ) ) { if ( i == j && path1 [ i ] == path2 [ j ] ) { i ++ ; j ++ ; } else { intersection = j - 1 ; break ; } } int sum = 0 ; for ( int i = path1 . size ( ) - 1 ; i > intersection ; i -- ) if ( path1 [ i ] % 2 ) sum += path1 [ i ] ; for ( int i = intersection ; i < path2 . size ( ) ; i ++ ) if ( path2 [ i ] % 2 ) sum += path2 [ i ] ; return sum ; } 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 -> right -> right = newNode ( 6 ) ; int node1 = 5 ; int node2 = 6 ; cout << sumOddNodes ( root , node1 , node2 ) ; return 0 ; }
Lowest Common Ancestor in Parent Array Representation | CPP program to find LCA in a tree represented as parent array . ; Maximum value in a node ; Function to find the Lowest common ancestor ; Create a visited vector and mark all nodes as not visited . ; Moving from n1 node till root and mark every accessed node as visited ; Move to the parent of node n1 ; For second node finding the first node common ; Insert function for Binary tree ; Driver Function ; Maximum capacity of binary tree ; Root marked
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; int findLCA ( int n1 , int n2 , int parent [ ] ) { vector < bool > visited ( MAX , false ) ; visited [ n1 ] = true ; while ( parent [ n1 ] != -1 ) { visited [ n1 ] = true ; n1 = parent [ n1 ] ; } visited [ n1 ] = true ; while ( ! visited [ n2 ] ) n2 = parent [ n2 ] ; return n2 ; } void insertAdj ( int parent [ ] , int i , int j ) { parent [ i ] = j ; } int main ( ) { int parent [ MAX ] ; parent [ 20 ] = -1 ; insertAdj ( parent , 8 , 20 ) ; insertAdj ( parent , 22 , 20 ) ; insertAdj ( parent , 4 , 8 ) ; insertAdj ( parent , 12 , 8 ) ; insertAdj ( parent , 10 , 12 ) ; insertAdj ( parent , 14 , 12 ) ; cout << findLCA ( 10 , 14 , parent ) ; return 0 ; }