codeparrot/xlcost-text-to-code huggingface dataset:
Check if an array is sorted and rotated using Binary Search |  ; Function to return the index of the pivot ; Base cases ; Check if element at ( mid - 1 ) is pivot Consider the cases like { 4 , 5 , 1 , 2 , 3 } ; Decide whether we need to go to the left half or the right half ; Function to check if a given array is sorted rotated or not ; To check if the elements to the left of the pivot are in descending or not ; To check if the elements to the right of the pivot are in ascending or not ; If both of the above if is true Then the array is sorted rotated ; Else the array is not sorted rotated ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findPivot ( int arr [ ] , int low , int high ) { if ( high < low ) return -1 ; if ( high == low ) return low ; int mid = ( low + high ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) { return mid ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return mid - 1 ; } if ( arr [ low ] > arr [ mid ] ) { return findPivot ( arr , low , mid - 1 ) ; } else { return findPivot ( arr , mid + 1 , high ) ; } } bool isRotated ( int arr [ ] , int n ) { int l = 0 ; int r = n - 1 ; int pivot = -1 ; if ( arr [ l ] > arr [ r ] ) { pivot = findPivot ( arr , l , r ) ; int temp = pivot ; if ( l < pivot ) { while ( pivot > l ) { if ( arr [ pivot ] < arr [ pivot - 1 ] ) { return false ; } pivot -- ; } } pivot = temp ; if ( pivot < r ) { pivot ++ ; while ( pivot < r ) { if ( arr [ pivot ] > arr [ pivot + 1 ] ) { return false ; } pivot ++ ; } } return true ; } else { return false ; } } int main ( ) { int arr [ ] = { 4 , 5 , 1 , 3 , 2 } ; if ( isRotated ( arr , 5 ) ) cout << " true " ; else cout << " false " ; return 0 ; }
Find a distinct pair ( x , y ) in given range such that x divides y | C ++ implementation of the approach ; Function to return the possible pair ; ans1 , ans2 store value of x and y respectively ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findpair ( int l , int r ) { int ans1 = l ; int ans2 = 2 * l ; cout << ans1 << " , ▁ " << ans2 << endl ; } int main ( ) { int l = 1 , r = 10 ; findpair ( l , r ) ; }
Jump in rank of a student after updating marks | C ++ implementation of the approach ; Function to print the name of student who stood first after updation in rank ; Array of students ; Store the name of the student ; Update the marks of the student ; Store the current rank of the student ; Print the name and jump in rank ; Driver code ; Names of the students ; Marks of the students ; Updates that are to be done ; Number of students
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Student { string name ; int marks = 0 ; int prev_rank = 0 ; } ; void nameRank ( string names [ ] , int marks [ ] , int updates [ ] , int n ) { Student x [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { x [ i ] . name = names [ i ] ; x [ i ] . marks = marks [ i ] + updates [ i ] ; x [ i ] . prev_rank = i + 1 ; } Student highest = x [ 0 ] ; for ( int j = 1 ; j < n ; j ++ ) { if ( x [ j ] . marks >= highest . marks ) highest = x [ j ] ; } cout << " Name : ▁ " << highest . name << " , ▁ Jump : ▁ " << abs ( highest . prev_rank - 1 ) << endl ; } int main ( ) { string names [ ] = { " sam " , " ram " , " geek " } ; int marks [ ] = { 80 , 79 , 75 } ; int updates [ ] = { 0 , 5 , -9 } ; int n = sizeof ( marks ) / sizeof ( marks [ 0 ] ) ; nameRank ( names , marks , updates , n ) ; }
Integers from the range that are composed of a single distinct digit | C ++ implementation of the approach ; Function to return the count of digits of a number ; Function to return a number that contains only digit ' d ' repeated exactly count times ; Function to return the count of integers that are composed of a single distinct digit only ; Count of digits in L and R ; First digits of L and R ; If L has lesser number of digits than R ; If the number that starts with firstDigitL and has number of digits = countDigitsL is within the range include the number ; Exclude the number ; If the number that starts with firstDigitR and has number of digits = countDigitsR is within the range include the number ; Exclude the number ; If both L and R have equal number of digits ; Include the number greater than L upto the maximum number whose digit = coutDigitsL ; Exclude the numbers which are greater than R ; Return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int n ) { int count = 0 ; while ( n > 0 ) { count += 1 ; n /= 10 ; } return count ; } int getDistinct ( int d , int count ) { int num = 0 ; count = pow ( 10 , count - 1 ) ; while ( count > 0 ) { num += ( count * d ) ; count /= 10 ; } return num ; } int findCount ( int L , int R ) { int count = 0 ; int countDigitsL = countDigits ( L ) ; int countDigitsR = countDigits ( R ) ; int firstDigitL = ( L / pow ( 10 , countDigitsL - 1 ) ) ; int firstDigitR = ( R / pow ( 10 , countDigitsR - 1 ) ) ; if ( countDigitsL < countDigitsR ) { count += ( 9 * ( countDigitsR - countDigitsL - 1 ) ) ; if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count += firstDigitR ; else count += ( firstDigitR - 1 ) ; } else { if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count -= ( 9 - firstDigitR ) ; else count -= ( 9 - firstDigitR + 1 ) ; } return count ; } int main ( ) { int L = 10 , R = 50 ; cout << findCount ( L , R ) ; return 0 ; }
Check if a pair with given absolute difference exists in a Matrix | CPP code to check for pair with given difference exists in the matrix or not ; Function to check if a pair with given difference exists in the matrix ; Store elements in a hash ; Loop to iterate over the elements of the matrix ; Driver Code ; Input matrix ; Given difference
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE #define M  4 NEW_LINE bool isPairWithDiff ( int mat [ N ] [ M ] , int k ) { unordered_set < int > ump ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] > k ) { int m = mat [ i ] [ j ] - k ; if ( ump . find ( m ) != ump . end ( ) ) { return true ; } } else { int m = k - mat [ i ] [ j ] ; if ( ump . find ( m ) != ump . end ( ) ) { return true ; } } ump . insert ( mat [ i ] [ j ] ) ; } } return false ; } int main ( ) { int mat [ N ] [ M ] = { { 5 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 100 } } ; int k = 85 ; if ( isPairWithDiff ( mat , k ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Next Smaller Element | A Stack based C ++ program to find next smaller element for all array elements ; prints NSE for elements of array arr [ ] of size n ; iterate for rest of the elementse ; if stack is not empty , then pop an element from stack . If the popped element is greater than next , then a ) print the pair b ) keep popping while elements are greater and stack is not empty ; push next to stack so that we can find NSE for it ; After iterating over the loop , the remaining elements in stack do not have any NSE , so set - 1 for them ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNSE ( int arr [ ] , int n ) { stack < pair < int , int > > s ; vector < int > ans ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { int next = arr [ i ] ; if ( s . empty ( ) ) { s . push ( { next , i } ) ; continue ; } while ( ! s . empty ( ) && s . top ( ) . first > next ) { ans [ s . top ( ) . second ] = next ; s . pop ( ) ; } s . push ( { next , i } ) ; } while ( ! s . empty ( ) ) { ans [ s . top ( ) . second ] = -1 ; s . pop ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ - - - > ▁ " << ans [ i ] << endl ; } } int main ( ) { int arr [ ] = { 11 , 13 , 21 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printNSE ( arr , n ) ; return 0 ; }
Deepest left leaf node in a binary tree | iterative approach | CPP program to find deepest left leaf node of binary tree ; tree node ; returns a new tree Node ; return the deepest left leaf node of binary tree ; create a queue for level order traversal ; traverse until the queue is empty ; Since we go level by level , the last stored left leaf node is deepest one , ; driver program ; construct a tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } Node * getDeepestLeftLeafNode ( Node * root ) { if ( ! root ) return NULL ; queue < Node * > q ; q . push ( root ) ; Node * result = NULL ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( temp -> left ) { q . push ( temp -> left ) ; if ( ! temp -> left -> left && ! temp -> left -> right ) result = temp -> left ; } if ( temp -> right ) q . push ( temp -> right ) ; } return result ; } 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 ( 6 ) ; root -> right -> left -> right = newNode ( 7 ) ; root -> right -> right -> right = newNode ( 8 ) ; root -> right -> left -> right -> left = newNode ( 9 ) ; root -> right -> right -> right -> right = newNode ( 10 ) ; Node * result = getDeepestLeftLeafNode ( root ) ; if ( result ) cout << " Deepest ▁ Left ▁ Leaf ▁ Node ▁ : : ▁ " << result -> data << endl ; else cout << " No ▁ result , ▁ left ▁ leaf ▁ not ▁ found STRNEWLINE " ; return 0 ; }
Find all possible binary trees with given Inorder Traversal | C ++ program to find binary tree with given inorder traversal ; Node structure ; A utility function to create a new tree Node ; A utility function to do preorder traversal of BST ; Function for constructing all possible trees with given inorder traversal stored in an array from arr [ start ] to arr [ end ] . This function returns a vector of trees . ; List to store all possible trees ; if start > end then subtree will be empty so returning NULL in the list ; Iterating through all values from start to end for constructing left and right subtree recursively ; Constructing left subtree ; Constructing right subtree ; Now looping through all left and right subtrees and connecting them to ith root below ; Making arr [ i ] as root ; Connecting left subtree ; Connecting right subtree ; Adding this tree to list ; Driver Program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; struct Node * newNode ( int item ) { struct Node * temp = new Node ; temp -> key = item ; temp -> left = temp -> right = NULL ; return temp ; } void preorder ( Node * root ) { if ( root != NULL ) { printf ( " % d ▁ " , root -> key ) ; preorder ( root -> left ) ; preorder ( root -> right ) ; } } vector < Node * > getTrees ( int arr [ ] , int start , int end ) { vector < Node * > trees ; if ( start > end ) { trees . push_back ( NULL ) ; return trees ; } for ( int i = start ; i <= end ; i ++ ) { vector < Node * > ltrees = getTrees ( arr , start , i - 1 ) ; vector < Node * > rtrees = getTrees ( arr , i + 1 , end ) ; for ( int j = 0 ; j < ltrees . size ( ) ; j ++ ) { for ( int k = 0 ; k < rtrees . size ( ) ; k ++ ) { Node * node = newNode ( arr [ i ] ) ; node -> left = ltrees [ j ] ; node -> right = rtrees [ k ] ; trees . push_back ( node ) ; } } } return trees ; } int main ( ) { int in [ ] = { 4 , 5 , 7 } ; int n = sizeof ( in ) / sizeof ( in [ 0 ] ) ; vector < Node * > trees = getTrees ( in , 0 , n - 1 ) ; cout << " Preorder ▁ traversals ▁ of ▁ different ▁ " << " possible ▁ Binary ▁ Trees ▁ are ▁ STRNEWLINE " ; for ( int i = 0 ; i < trees . size ( ) ; i ++ ) { preorder ( trees [ i ] ) ; printf ( " STRNEWLINE " ) ; } return 0 ; }
Check if a string is suffix of another | CPP program to find if a string is suffix of another ; Test case - sensitive implementation of endsWith function
#include <boost/algorithm/string.hpp> NEW_LINE #include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int main ( ) { string s1 = " geeks " , s2 = " geeksforgeeks " ; bool result = boost :: algorithm :: ends_with ( s2 , s1 ) ; if ( result ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximum elements that can be made equal with k updates | C ++ program to find maximum elements that can be made equal with k updates ; Function to calculate the maximum number of equal elements possible with atmost K increment of values . Here we have done sliding window to determine that whether there are x number of elements present which on increment will become equal . The loop here will run in fashion like 0. . . x - 1 , 1. . . x , 2. . . x + 1 , ... . , n - x - 1. . . n - 1 ; It can be explained with the reasoning that if for some x number of elements we can update the values then the increment to the segment ( i to j having length -> x ) so that all will be equal is ( x * maxx [ j ] ) this is the total sum of segment and ( pre [ j ] - pre [ i ] ) is present sum So difference of them should be less than k if yes , then that segment length ( x ) can be possible return true ; sort the array in ascending order ; Initializing the prefix array and maximum array ; Calculating prefix sum of the array ; Calculating max value upto that position in the array ; Binary search applied for computation here ; printing result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ElementsCalculationFunc ( int pre [ ] , int maxx [ ] , int x , int k , int n ) { for ( int i = 0 , j = x ; j <= n ; j ++ , i ++ ) { if ( x * maxx [ j ] - ( pre [ j ] - pre [ i ] ) <= k ) return true ; } return false ; } void MaxNumberOfElements ( int a [ ] , int n , int k ) { sort ( a , a + n ) ; for ( int i = 0 ; i <= n ; ++ i ) { pre [ i ] = 0 ; maxx [ i ] = 0 ; } for ( int i = 1 ; i <= n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i - 1 ] ; maxx [ i ] = max ( maxx [ i - 1 ] , a [ i - 1 ] ) ; } int l = 1 , r = n , ans ; while ( l < r ) { int mid = ( l + r ) / 2 ; if ( ElementsCalculationFunc ( pre , maxx , mid - 1 , k , n ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } cout << ans << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 2 , 4 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; MaxNumberOfElements ( arr , n , k ) ; return 0 ; }
Count palindromic characteristics of a String | C ++ program which counts different palindromic characteristics of a string . ; function which checks whether a substring str [ i . . j ] of a given string is a palindrome or not . ; P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false ; palindrome of single length ; palindrome of length 2 ; Palindromes of length more then 2. This loop is similar to Matrix Chain Multiplication . We start with a gap of length 2 and fill P table in a way that gap between starting and ending indexes increases one by one by outer loop . ; Pick starting point for current gap ; Set ending point ; If current string is palindrome ; function which recursively counts if a string str [ i . . j ] is a k - palindromic string or not . ; terminating condition for a string which is a k - palindrome . ; terminating condition for a string which is not a k - palindrome . ; increases the counter for the string if it is a k - palindrome . ; mid is middle pointer of the string str [ i ... j ] . ; if length of string which is ( j - i + 1 ) is odd than we have to subtract one from mid . else if even then no change . ; if the string is k - palindrome then we check if it is a ( k + 1 ) - palindrome or not by just sending any of one half of the string to the Count_k_Palindrome function . ; Finding all palindromic substrings of given string ; counting k - palindromes for each and every substring of given string . . ; Output the number of K - palindromic substrings of a given string . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_STR_LEN = 1000 ; bool P [ MAX_STR_LEN ] [ MAX_STR_LEN ] ; int Kpal [ MAX_STR_LEN ] ; void checkSubStrPal ( string str , int n ) { memset ( P , false , sizeof ( P ) ) ; for ( int i = 0 ; i < n ; i ++ ) P [ i ] [ i ] = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( str [ i ] == str [ i + 1 ] ) P [ i ] [ i + 1 ] = true ; for ( int gap = 2 ; gap < n ; gap ++ ) { for ( int i = 0 ; i < n - gap ; i ++ ) { int j = gap + i ; if ( str [ i ] == str [ j ] && P [ i + 1 ] [ j - 1 ] ) P [ i ] [ j ] = true ; } } } void countKPalindromes ( int i , int j , int k ) { if ( i == j ) { Kpal [ k ] ++ ; return ; } if ( P [ i ] [ j ] == false ) return ; Kpal [ k ] ++ ; int mid = ( i + j ) / 2 ; if ( ( j - i + 1 ) % 2 == 1 ) mid -- ; countKPalindromes ( i , mid , k + 1 ) ; } void printKPalindromes ( string s ) { memset ( Kpal , 0 , sizeof ( Kpal ) ) ; int n = s . length ( ) ; checkSubStrPal ( s , n ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - i ; j ++ ) countKPalindromes ( j , j + i , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) cout << Kpal [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } int main ( ) { string s = " abacaba " ; printKPalindromes ( s ) ; return 0 ; }
Print number in ascending order which contains 1 , 2 and 3 in their digits . | CPP program to print all number containing 1 , 2 and 3 in any order . ; prints all the number containing 1 , 2 , 3 ; check if the number contains 1 , 2 & 3 in any order ; sort all the numbers ; convert the number to string and find if it contains 1 , 2 & 3. ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string printNumbers ( int numbers [ ] , int n ) { vector < int > oneTwoThree ; for ( int i = 0 ; i < n ; i ++ ) { if ( findContainsOneTwoThree ( numbers [ i ] ) ) oneTwoThree . push_back ( numbers [ i ] ) ; } sort ( oneTwoThree . begin ( ) , oneTwoThree . end ( ) ) ; string result = " " ; for ( auto number : oneTwoThree ) { int value = number ; if ( result . length ( ) > 0 ) result += " , ▁ " ; result += to_string ( value ) ; } return ( result . length ( ) > 0 ) ? result : " - 1" ; } bool findContainsOneTwoThree ( int number ) { string str = to_string ( number ) ; int countOnes = 0 , countTwo = 0 , countThree = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == '1' ) countOnes ++ ; else if ( str [ i ] == '2' ) countTwo ++ ; else if ( str [ i ] == '3' ) countThree ++ ; } return ( countOnes && countTwo && countThree ) ; } int main ( ) { int numbers [ ] = { 123 , 1232 , 456 , 234 , 32145 } ; int n = sizeof ( numbers ) / sizeof ( numbers [ 0 ] ) ; string result = printNumbers ( numbers , n ) ; cout << result ; return 0 ; }
Find k | C ++ implementation to solve k queries for given n ranges ; Structure to store the start and end point ; Comparison function for sorting ; Function to find Kth smallest number in a vector of merged intervals ; Traverse merged [ ] to find Kth smallest element using Linear search . ; To combined both type of ranges , overlapping as well as non - overlapping . ; Sorting intervals according to start time ; Merging all intervals into merged ; To check if starting point of next range is lying between the previous range and ending point of next range is greater than the Ending point of previous range then update ending point of previous range by ending point of next range . ; If starting point of next range is greater than the ending point of previous range then store next range in merged [ ] . ; Driver \'s Function ; Merge all intervals into merged [ ] ; Processing all queries on merged intervals
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Interval { int s ; int e ; } ; bool comp ( Interval a , Interval b ) { return a . s < b . s ; } int kthSmallestNum ( vector < Interval > merged , int k ) { int n = merged . size ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( k <= abs ( merged [ j ] . e - merged [ j ] . s + 1 ) ) return ( merged [ j ] . s + k - 1 ) ; k = k - abs ( merged [ j ] . e - merged [ j ] . s + 1 ) ; } if ( k ) return -1 ; } void mergeIntervals ( vector < Interval > & merged , Interval arr [ ] , int n ) { sort ( arr , arr + n , comp ) ; merged . push_back ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { Interval prev = merged . back ( ) ; Interval curr = arr [ i ] ; if ( ( curr . s >= prev . s && curr . s <= prev . e ) && ( curr . e > prev . e ) ) merged . back ( ) . e = curr . e ; else { if ( curr . s > prev . e ) merged . push_back ( curr ) ; } } } int main ( ) { Interval arr [ ] = { { 2 , 6 } , { 4 , 7 } } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int query [ ] = { 5 , 8 } ; int q = sizeof ( query ) / sizeof ( query [ 0 ] ) ; vector < Interval > merged ; mergeIntervals ( merged , arr , n ) ; for ( int i = 0 ; i < q ; i ++ ) cout << kthSmallestNum ( merged , query [ i ] ) << endl ; return 0 ; }
Grouping Countries | CPP program to count no of distinct countries from a given group of people ; Answer is valid if adjacent sitting num people give same answer ; someone gives different answer ; check next person ; one valid country group has been found ; driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void countCountries ( int ans [ ] , int N ) { int total_countries = 0 , i = 0 ; bool invalid = false ; while ( i < N ) { int curr_size = ans [ i ] ; int num = ans [ i ] ; while ( num > 0 ) { if ( ans [ i ] != curr_size ) { cout << " Invalid ▁ Answer STRNEWLINE " ; return ; } else num -- ; i ++ ; } total_countries ++ ; } cout << " There ▁ are ▁ " << total_countries << " ▁ distinct ▁ companies ▁ in ▁ the ▁ group . STRNEWLINE " ; } int main ( ) { int ans [ ] = { 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 } ; int n = sizeof ( ans ) / sizeof ( ans [ 0 ] ) ; countCountries ( ans , n ) ; return 0 ; }
Deepest left leaf node in a binary tree | A C ++ program to find the deepest left leaf in a given binary tree ; A Binary Tree node ; New node ; A utility function to find deepest leaf node . lvl : level of current node . maxlvl : pointer to the deepest left leaf node found so far isLeft : A bool indicate that this node is left child of its parent resPtr : Pointer to the result ; Base case ; Update result if this node is left leaf and its level is more than the maxl level of the current result ; Recur for left and right subtrees ; A wrapper over deepestLeftLeafUtil ( ) . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int val ; struct Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> val = data ; temp -> left = temp -> right = NULL ; return temp ; } void deepestLeftLeafUtil ( Node * root , int lvl , int * maxlvl , bool isLeft , Node * * resPtr ) { if ( root == NULL ) return ; if ( isLeft && ! root -> left && ! root -> right && lvl > * maxlvl ) { * resPtr = root ; * maxlvl = lvl ; return ; } deepestLeftLeafUtil ( root -> left , lvl + 1 , maxlvl , true , resPtr ) ; deepestLeftLeafUtil ( root -> right , lvl + 1 , maxlvl , false , resPtr ) ; } Node * deepestLeftLeaf ( Node * root ) { int maxlevel = 0 ; Node * result = NULL ; deepestLeftLeafUtil ( root , 0 , & maxlevel , false , & result ) ; return result ; } 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 ( 6 ) ; root -> right -> left -> right = newNode ( 7 ) ; root -> right -> right -> right = newNode ( 8 ) ; root -> right -> left -> right -> left = newNode ( 9 ) ; root -> right -> right -> right -> right = newNode ( 10 ) ; Node * result = deepestLeftLeaf ( root ) ; if ( result ) cout << " The ▁ deepest ▁ left ▁ child ▁ is ▁ " << result -> val ; else cout << " There ▁ is ▁ no ▁ left ▁ leaf ▁ in ▁ the ▁ given ▁ tree " ; return 0 ; }