codeparrot/xlcost-text-to-code huggingface dataset:
Find depth of the deepest odd level leaf node | C ++ program to find depth of the deepest odd level leaf node ; A utility function to find maximum of two integers ; A Binary Tree node ; A utility function to allocate a new tree node ; A recursive function to find depth of the deepest odd level leaf ; Base Case ; If this node is a leaf and its level is odd , return its level ; If not leaf , return the maximum value from left and right subtrees ; Main function which calculates the depth of deepest odd level leaf . This function mainly uses depthOfOddLeafUtil ( ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max ( int x , int y ) { return ( x > y ) ? x : y ; } struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } int depthOfOddLeafUtil ( struct Node * root , int level ) { if ( root == NULL ) return 0 ; if ( root -> left == NULL && root -> right == NULL && level & 1 ) return level ; return max ( depthOfOddLeafUtil ( root -> left , level + 1 ) , depthOfOddLeafUtil ( root -> right , level + 1 ) ) ; } int depthOfOddLeaf ( struct Node * root ) { int level = 1 , depth = 0 ; return depthOfOddLeafUtil ( root , level ) ; } int main ( ) { struct 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 ) ; root -> right -> right -> right -> right -> left = newNode ( 11 ) ; cout << depthOfOddLeaf ( root ) << " ▁ is ▁ the ▁ required ▁ depth " ; getchar ( ) ; return 0 ; }
Check if an array can be split into subarrays with GCD exceeding K | C ++ program for the above approach ; Function to check if it is possible to split an array into subarrays having GCD at least K ; Iterate over the array arr [ ] ; If the current element is less than or equal to k ; If no array element is found to be less than or equal to k ; Driver Code ; Given array arr [ ] ; Length of the array ; Given K ; Function Call
#include <iostream> NEW_LINE using namespace std ; string canSplitArray ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= k ) { return " No " ; } } return " Yes " ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 1 , 8 , 16 } ; int N = sizeof arr / sizeof arr [ 0 ] ; int N = sizeof arr / sizeof arr [ 0 ] ; int K = 3 ; cout << canSplitArray ( arr , N , K ) ; }
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | C ++ 14 program for the above approach ; Function to find the smallest subarray sum greater than or equal to target ; Base Case ; If sum of the array is less than target ; If target is equal to the sum of the array ; Required condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallSumSubset ( vector < int > data , int target , int maxVal ) { int sum = 0 ; for ( int i : data ) sum += i ; if ( target <= 0 ) return 0 ; else if ( sum < target ) return maxVal ; else if ( sum == target ) return data . size ( ) ; else if ( data [ 0 ] >= target ) return 1 ; else if ( data [ 0 ] < target ) { vector < int > temp ; for ( int i = 1 ; i < data . size ( ) ; i ++ ) temp . push_back ( data [ i ] ) ; return min ( smallSumSubset ( temp , target , maxVal ) , 1 + smallSumSubset ( temp , target - data [ 0 ] , maxVal ) ) ; } } int main ( ) { vector < int > data = { 3 , 1 , 7 , 1 , 2 } ; int target = 11 ; int val = smallSumSubset ( data , target , data . size ( ) + 1 ) ; if ( val > data . size ( ) ) cout << -1 ; else cout << val ; }
Maximize cost obtained by removal of substrings " pr " or " rp " from a given String | C ++ Program to implement the above approach ; Function to maintain the case , X >= Y ; To maintain X >= Y ; Replace ' p ' to ' r ' ; Replace ' r ' to ' p ' . ; Function to return the maximum cost ; Stores the length of the string ; To maintain X >= Y . ; Stores the maximum cost ; Stores the count of ' p ' after removal of all " pr " substrings up to str [ i ] ; Stores the count of ' r ' after removal of all " pr " substrings up to str [ i ] ; Stack to maintain the order of characters after removal of substrings ; If substring " pr " is removed ; Increase cost by X ; If any substring " rp " left in the Stack ; If any substring " rp " left in the Stack ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool swapXandY ( string & str , int X , int Y ) { int N = str . length ( ) ; swap ( X , Y ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' p ' ) { str [ i ] = ' r ' ; } else if ( str [ i ] == ' r ' ) { str [ i ] = ' p ' ; } } } int maxCost ( string str , int X , int Y ) { int N = str . length ( ) ; if ( Y > X ) { swapXandY ( str , X , Y ) ; } int res = 0 ; int countP = 0 ; int countR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' p ' ) { countP ++ ; } else if ( str [ i ] == ' r ' ) { if ( countP > 0 ) { countP -- ; res += X ; } else countR ++ ; } else { res += min ( countP , countR ) * Y ; countP = 0 ; countR = 0 ; } } res += min ( countP , countR ) * Y ; return res ; } int main ( ) { string str = " abppprrr " ; int X = 5 , Y = 4 ; cout << maxCost ( str , X , Y ) ; }
Check if an array contains only one distinct element | C ++ program for the above approach ; Function to find if the array contains only one distinct element ; Assume first element to be the unique element ; Traversing the array ; If current element is not equal to X then break the loop and print No ; Compare and print the result ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void uniqueElement ( int arr [ ] , int n ) { int x = arr [ 0 ] ; int flag = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != x ) { flag = 0 ; break ; } } if ( flag == 1 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int arr [ ] = { 9 , 9 , 9 , 9 , 9 , 9 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; uniqueElement ( arr , n ) ; }
Maximum Count of pairs having equal Sum based on the given conditions | C ++ Program to implement the above approach ; Function to find the maximum count of pairs having equal sum ; Size of the array ; Iterate through evey sum of pairs possible from the given array ; Count of pairs with given sum ; Check for a possible pair ; Update count of possible pair ; Update the answer by taking the pair which is maximum for every possible sum ; Return the max possible pair ; Function to return the count of pairs ; Size of the array ; Stores the frequencies ; Count the frequency ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxCount ( vector < int > & freq , int mini , int maxi ) { int n = freq . size ( ) - 1 ; int ans = 0 ; for ( int sum = 2 * mini ; sum <= 2 * maxi ; ++ sum ) { int possiblePair = 0 ; for ( int firElement = 1 ; firElement < ( sum + 1 ) / 2 ; firElement ++ ) { if ( sum - firElement <= maxi ) { possiblePair += min ( freq [ firElement ] , freq [ sum - firElement ] ) ; } } if ( sum % 2 == 0 ) { possiblePair += freq [ sum / 2 ] / 2 ; } ans = max ( ans , possiblePair ) ; } return ans ; } int countofPairs ( vector < int > & a ) { int n = a . size ( ) ; int mini = * min_element ( a . begin ( ) , a . end ( ) ) , maxi = * max_element ( a . begin ( ) , a . end ( ) ) ; vector < int > freq ( n + 1 , 0 ) ; for ( int i = 0 ; i < n ; ++ i ) freq [ a [ i ] ] ++ ; return maxCount ( freq , mini , maxi ) ; } int main ( ) { vector < int > a = { 1 , 2 , 4 , 3 , 3 , 5 , 6 } ; cout << countofPairs ( a ) << endl ; }
Maximum Subarray Sum possible by replacing an Array element by its Square | C ++ program to implement the above approach ; Function to find the maximum subarray sum possible ; Stores sum without squaring ; Stores sum squaring ; Stores the maximum subarray sum ; Either extend the subarray or start a new subarray ; Either extend previous squared subarray or start a new subarray by squaring the current element ; Update maximum subarray sum ; Return answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaxSum ( int a [ ] , int n ) { int dp [ n ] [ 2 ] ; dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = a [ 0 ] * a [ 0 ] ; int max_sum = max ( dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = max ( a [ i ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) ; dp [ i ] [ 1 ] = max ( dp [ i - 1 ] [ 1 ] + a [ i ] , a [ i ] * a [ i ] ) ; dp [ i ] [ 1 ] = max ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + a [ i ] * a [ i ] ) ; max_sum = max ( max_sum , dp [ i ] [ 1 ] ) ; max_sum = max ( max_sum , dp [ i ] [ 0 ] ) ; } return max_sum ; } int32_t main ( ) { int n = 5 ; int a [ ] = { 1 , -5 , 8 , 12 , -8 } ; cout << getMaxSum ( a , n ) << endl ; return 0 ; }
Smallest Subarray with Sum K from an Array | C ++ Program to implement the above approach ; Function to find the length of the smallest subarray with sum K ; Stores the frequency of prefix sums in the array ; Initialize len as INT_MAX ; If sum of array till i - th index is less than K ; No possible subarray exists till i - th index ; Find the exceeded value ; If exceeded value is zero ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subArraylen ( int arr [ ] , int n , int K ) { unordered_map < int , int > mp ; mp [ arr [ 0 ] ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; mp [ arr [ i ] ] = i ; } int len = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < K ) continue ; else { int x = arr [ i ] - K ; if ( x == 0 ) len = min ( len , i ) ; if ( mp . find ( x ) == mp . end ( ) ) continue ; else { len = min ( len , i - mp [ x ] ) ; } } } return len ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 3 , 2 , 4 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 7 ; int len = subArraylen ( arr , n , K ) ; if ( len == INT_MAX ) { cout << " - 1" ; } else { cout << len << endl ; } return 0 ; }
Find depth of the deepest odd level leaf node | CPP program to find depth of the deepest odd level leaf node of binary tree ; tree node ; returns a new tree Node ; return max odd number depth of leaf node ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if the node is leaf node and level is odd if level is odd , then update result ; check for left child ; check for right child ; 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 ; } int maxOddLevelDepth ( Node * root ) { if ( ! root ) return 0 ; queue < Node * > q ; q . push ( root ) ; int result = INT_MAX ; int level = 0 ; while ( ! q . empty ( ) ) { int size = q . size ( ) ; level += 1 ; while ( size > 0 ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( ! temp -> left && ! temp -> right && ( level % 2 != 0 ) ) { result = level ; } if ( temp -> left ) { q . push ( temp -> left ) ; } if ( temp -> right ) { q . push ( temp -> right ) ; } size -= 1 ; } } 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 ) ; root -> right -> right -> right -> right -> left = newNode ( 11 ) ; int result = maxOddLevelDepth ( root ) ; if ( result == INT_MAX ) cout << " No ▁ leaf ▁ node ▁ at ▁ odd ▁ level STRNEWLINE " ; else cout << result ; cout << " ▁ is ▁ the ▁ required ▁ depth ▁ " << endl ; return 0 ; }
Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range | C ++ program to implement the above approach ; Stores at each index if it 's a prime or not ; Sieve of Eratosthenes to generate all possible primes ; If p is a prime ; Set all multiples of p as non - prime ; Function to generate the required Fibonacci Series ; Stores all primes between n1 and n2 ; Generate all primes between n1 and n2 ; Stores all concatenations of each pair of primes ; Generate all concatenations of each pair of primes ; Stores the primes out of the numbers generated above ; Store the unique primes ; Find the minimum ; Find the minimum ; Find N ; Print the N - th term of the Fibonacci Series ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int  long long int NEW_LINE int prime [ 100001 ] ; void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < 100001 ; i ++ ) prime [ i ] = 1 ; int p = 2 ; while ( p * p <= 100000 ) { if ( prime [ p ] == 1 ) { for ( int i = p * p ; i < 100001 ; i += p ) prime [ i ] = 0 ; } p += 1 ; } } int join ( int a , int b ) { int mul = 1 ; int bb = b ; while ( b != 0 ) { mul *= 10 ; b /= 10 ; } a *= mul ; a += bb ; return a ; } void fibonacciOfPrime ( int n1 , int n2 ) { SieveOfEratosthenes ( ) ; vector < int > initial ; for ( int i = n1 ; i <= n2 ; i ++ ) if ( prime [ i ] ) initial . push_back ( i ) ; vector < int > now ; for ( auto a : initial ) for ( auto b : initial ) if ( a != b ) { int c = join ( a , b ) ; now . push_back ( c ) ; } vector < int > current ; for ( auto x : now ) if ( prime [ x ] ) current . push_back ( x ) ; set < int > current_set ; for ( auto i : current ) current_set . insert ( i ) ; int first = * min_element ( current_set . begin ( ) , current_set . end ( ) ) ; int second = * max_element ( current_set . begin ( ) , current_set . end ( ) ) ; int count = current_set . size ( ) - 1 ; int curr = 1 ; int c ; while ( curr < count ) { c = first + second ; first = second ; second = c ; curr += 1 ; } cout << ( c ) << endl ; } int32_t main ( ) { int x = 2 ; int y = 40 ; fibonacciOfPrime ( x , y ) ; }
Count of Array elements greater than all elements on its left and next K elements on its right | C ++ Program to implement the above approach ; Function to print the count of Array elements greater than all elements on its left and next K elements on its right ; Iterate over the array ; If the stack is not empty and the element at the top of the stack is smaller than arr [ i ] ; Store the index of next greater element ; Pop the top element ; Insert the current index ; Stores the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countElements ( int arr [ ] , int n , int k ) { stack < int > s ; vector < int > next_greater ( n , n + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . empty ( ) ) { s . push ( i ) ; continue ; } while ( ! s . empty ( ) && arr [ s . top ( ) ] < arr [ i ] ) { next_greater [ s . top ( ) ] = i ; s . pop ( ) ; } s . push ( i ) ; } int count = 0 ; int maxi = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( next_greater [ i ] - i > k && maxi < arr [ i ] ) { maxi = max ( maxi , arr [ i ] ) ; count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 4 , 2 , 3 , 6 , 4 , 3 , 2 } ; int K = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countElements ( arr , n , K ) ; return 0 ; }
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | C ++ program to find the maximum count of 1 s ; If arr [ i - 2 ] = = 1 then we increment the count of occurences of 1 's ; else we initialise the count with 0 ; If arr [ i + 2 ] = = 1 then we increment the count of occurences of 1 's ; else we initialise the count with 0 ; We get the maximum count by skipping the current and the next element . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxLengthOf1s ( vector < int > arr , int n ) { vector < int > prefix ( n , 0 ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i - 2 ] == 1 ) prefix [ i ] = prefix [ i - 1 ] + 1 ; else prefix [ i ] = 0 ; } vector < int > suffix ( n , 0 ) ; for ( int i = n - 3 ; i >= 0 ; i -- ) { if ( arr [ i + 2 ] == 1 ) suffix [ i ] = suffix [ i + 1 ] + 1 ; else suffix [ i ] = 0 ; } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans = max ( ans , prefix [ i + 1 ] + suffix [ i ] ) ; } cout << ans << " STRNEWLINE " ; } int main ( ) { int n = 6 ; vector < int > arr = { 1 , 1 , 1 , 0 , 1 , 1 } ; maxLengthOf1s ( arr , n ) ; return 0 ; }
Minimum number of operations required to obtain a given Binary String | C ++ Program to implement the above approach ; Function to find the minimum number of operations required to obtain the string s ; Iterate the string s ; If first occurrence of 1 is found ; Mark the index ; Base case : If no 1 occurred ; No operations required ; Stores the character for which last operation was performed ; Stores minimum number of operations ; Iterate from pos to n ; Check if s [ i ] is 0 ; Check if last operation was performed because of 1 ; Set last to 0 ; Check if last operation was performed because of 0 ; Set last to 1 ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string s ) { int n = s . size ( ) ; int pos = -1 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == '1' ) { pos = i ; break ; } } if ( pos == -1 ) { return 0 ; } int last = 1 ; int ans = 1 ; for ( int i = pos + 1 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { if ( last == 1 ) { ans ++ ; last = 0 ; } } else { if ( last == 0 ) { ans ++ ; last = 1 ; } } } return ans ; } int main ( ) { string s = "10111" ; cout << minOperations ( s ) ; return 0 ; }
Find the Deepest Node in a Binary Tree | A C ++ program to find value of the deepest node in a given binary tree ; A tree node ; Utility function to create a new node ; maxLevel : keeps track of maximum level seen so far . res : Value of deepest node so far . level : Level of root ; Update level and resue ; Returns value of deepest node ; Initialze result and max level ; Updates value " res " and " maxLevel " Note that res and maxLen are passed by reference . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void find ( Node * root , int level , int & maxLevel , int & res ) { if ( root != NULL ) { find ( root -> left , ++ level , maxLevel , res ) ; if ( level > maxLevel ) { res = root -> data ; maxLevel = level ; } find ( root -> right , level , maxLevel , res ) ; } } int deepestNode ( Node * root ) { int res = -1 ; int maxLevel = -1 ; find ( root , 0 , maxLevel , res ) ; return res ; } 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 ) ; cout << deepestNode ( root ) ; return 0 ; }
Count the number of clumps in the given Array | C ++ program to calculate the number of clumps in an array ; Function to count the number of clumps in the given array arr [ ] ; Initialise count of clumps as 0 ; Traverse the arr [ ] ; Whenever a sequence of same value is encountered ; Return the count of clumps ; Driver Code ; length of the given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countClumps ( int arr [ ] , int N ) { int clumps = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int flag = 0 ; while ( arr [ i ] == arr [ i + 1 ] ) { flag = 1 ; i ++ ; } if ( flag ) clumps ++ ; } return clumps ; } int main ( ) { int arr [ ] = { 13 , 15 , 66 , 66 , 66 , 37 , 37 , 8 , 8 , 11 , 11 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countClumps ( arr , N ) << ' ' ; return 0 ; }
Find if string is K | C ++ program to find if string is K - Palindrome or not using all characters exactly once ; when size of string is less than k ; when size of string is equal to k ; when size of string is greater than k to store the frequencies of the characters ; to store the count of characters whose number of occurrences is odd . ; iterating over the map ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void iskPalindromesPossible ( string s , int k ) { if ( s . size ( ) < k ) { cout << " Not ▁ Possible " << endl ; return ; } if ( s . size ( ) == k ) { cout << " Possible " << endl ; return ; } map < char , int > freq ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) freq [ s [ i ] ] ++ ; int count = 0 ; for ( auto it : freq ) { if ( it . second % 2 == 1 ) count ++ ; } if ( count > k ) cout << " No " << endl ; else cout << " Yes " << endl ; } int main ( ) { string str = " poor " ; int K = 3 ; iskPalindromesPossible ( str , K ) ; str = " geeksforgeeks " ; K = 10 ; iskPalindromesPossible ( str , K ) ; return 0 ; }
Find the Deepest Node in a Binary Tree | A C ++ program to find value of the deepest node in a given binary tree ; A tree node with constructor ; Utility function to find height of a tree , rooted at ' root ' . ; levels : current Level Utility function to print all nodes at a given level . ; Driver program ; Calculating height of tree ; Printing the deepest node
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * left , * right ; Node ( int key ) { data = key ; left = NULL ; right = NULL ; } } ; int height ( Node * root ) { if ( ! root ) return 0 ; int leftHt = height ( root -> left ) ; int rightHt = height ( root -> right ) ; return max ( leftHt , rightHt ) + 1 ; } void deepestNode ( Node * root , int levels ) { if ( ! root ) return ; if ( levels == 1 ) cout << root -> data ; else if ( levels > 1 ) { deepestNode ( root -> left , levels - 1 ) ; deepestNode ( root -> right , levels - 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 -> right -> left = new Node ( 5 ) ; root -> right -> right = new Node ( 6 ) ; root -> right -> left -> right = new Node ( 7 ) ; root -> right -> right -> right = new Node ( 8 ) ; root -> right -> left -> right -> left = new Node ( 9 ) ; int levels = height ( root ) ; deepestNode ( root , levels ) ; return 0 ; }