codeparrot/xlcost-text-to-code huggingface dataset:
Find depth of the deepest odd level leaf node | A binary 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
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int depthOfOddLeafUtil ( Node node , int level ) { if ( node == null ) return 0 ; if ( node . left == null && node . right == null && ( level & 1 ) != 0 ) return level ; return Math . max ( depthOfOddLeafUtil ( node . left , level + 1 ) , depthOfOddLeafUtil ( node . right , level + 1 ) ) ; } int depthOfOddLeaf ( Node node ) { int level = 1 , depth = 0 ; return depthOfOddLeafUtil ( node , level ) ; } public static void main ( String args [ ] ) { int k = 45 ; BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . right . left . right = new Node ( 7 ) ; tree . root . right . right . right = new Node ( 8 ) ; tree . root . right . left . right . left = new Node ( 9 ) ; tree . root . right . right . right . right = new Node ( 10 ) ; tree . root . right . right . right . right . left = new Node ( 11 ) ; System . out . println ( tree . depthOfOddLeaf ( tree . root ) + " ▁ is ▁ the ▁ required ▁ depth " ) ; } }
Check if an array can be split into subarrays with GCD exceeding K | Java 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
import java . io . * ; class GFG { static String canSplitArray ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= k ) { return " No " ; } } return " Yes " ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 1 , 8 , 16 } ; int N = arr . length ; int K = 3 ; System . out . println ( canSplitArray ( arr , N , K ) ) ; } }
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | Java 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
import java . util . * ; import java . lang . * ; class GFG { static int smallSumSubset ( List < Integer > data , int target , int maxVal ) { int sum = 0 ; for ( Integer 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 . get ( 0 ) >= target ) return 1 ; else if ( data . get ( 0 ) < target ) { List < Integer > temp = new ArrayList < > ( ) ; for ( int i = 1 ; i < data . size ( ) ; i ++ ) temp . add ( data . get ( i ) ) ; return Math . min ( smallSumSubset ( temp , target , maxVal ) , 1 + smallSumSubset ( temp , target - data . get ( 0 ) , maxVal ) ) ; } return - 1 ; } public static void main ( String [ ] args ) { List < Integer > data = Arrays . asList ( 3 , 1 , 7 , 1 , 2 ) ; int target = 11 ; int val = smallSumSubset ( data , target , data . size ( ) + 1 ) ; if ( val > data . size ( ) ) System . out . println ( - 1 ) ; else System . out . println ( val ) ; } }
Maximize cost obtained by removal of substrings " pr " or " rp " from a given String | Java 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
import java . util . * ; class GFG { static boolean swapXandY ( char [ ] str , int X , int Y ) { int N = str . length ; X = X + Y ; Y = X - Y ; X = X - Y ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' p ' ) { str [ i ] = ' r ' ; } else if ( str [ i ] == ' r ' ) { str [ i ] = ' p ' ; } } return true ; } static int maxCost ( String str , int X , int Y ) { int N = str . length ( ) ; if ( Y > X ) { swapXandY ( str . toCharArray ( ) , X , Y ) ; } int res = 0 ; int countP = 0 ; int countR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' p ' ) { countP ++ ; } else if ( str . charAt ( i ) == ' r ' ) { if ( countP > 0 ) { countP -- ; res += X ; } else countR ++ ; } else { res += Math . min ( countP , countR ) * Y ; countP = 0 ; countR = 0 ; } } res += Math . min ( countP , countR ) * Y ; return res ; } public static void main ( String [ ] args ) { String str = " abppprrr " ; int X = 5 , Y = 4 ; System . out . print ( maxCost ( str , X , Y ) ) ; } }
Check if an array contains only one distinct element | Java 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
import java . util . * ; public class Main { public static void uniqueElement ( int arr [ ] ) { int x = arr [ 0 ] ; int flag = 1 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] != x ) { flag = 0 ; break ; } } if ( flag == 1 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 9 , 9 , 9 , 9 , 9 , 9 , 9 } ; uniqueElement ( arr ) ; } }
Maximum Count of pairs having equal Sum based on the given conditions | Java 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
class GFG { static int maxCount ( int [ ] freq , int maxi , int mini ) { int n = freq . length - 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 += Math . min ( freq [ firElement ] , freq [ sum - firElement ] ) ; } } if ( sum % 2 == 0 ) { possiblePair += freq [ sum / 2 ] / 2 ; } ans = Math . max ( ans , possiblePair ) ; } return ans ; } static int countofPairs ( int [ ] a ) { int n = a . length ; int [ ] freq = new int [ n + 1 ] ; int maxi = - 1 ; int mini = n + 1 ; for ( int i = 0 ; i < n ; i ++ ) { maxi = Math . max ( maxi , a [ i ] ) ; mini = Math . min ( mini , a [ i ] ) ; } for ( int i = 0 ; i < n ; ++ i ) freq [ a [ i ] ] ++ ; return maxCount ( freq , maxi , mini ) ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 2 , 4 , 3 , 3 , 5 , 6 } ; System . out . print ( countofPairs ( a ) + "NEW_LINE"); } }
Maximum Subarray Sum possible by replacing an Array element by its Square | Java 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
import java . io . * ; class GFG { public static int getMaxSum ( int a [ ] , int n ) { int dp [ ] [ ] = new int [ n ] [ 2 ] ; dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = a [ 0 ] * a [ 0 ] ; int max_sum = Math . max ( dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( a [ i ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) ; dp [ i ] [ 1 ] = Math . max ( dp [ i - 1 ] [ 1 ] + a [ i ] , a [ i ] * a [ i ] ) ; dp [ i ] [ 1 ] = Math . max ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + a [ i ] * a [ i ] ) ; max_sum = Math . max ( max_sum , dp [ i ] [ 1 ] ) ; max_sum = Math . max ( max_sum , dp [ i ] [ 0 ] ) ; } return max_sum ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 1 , - 5 , 8 , 12 , - 8 } ; System . out . println ( getMaxSum ( a , n ) ) ; } }
Smallest Subarray with Sum K from an Array | Java 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 Integer . MAX_VALUE ; 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
import java . util . * ; class GFG { static int subArraylen ( int arr [ ] , int n , int K ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; mp . put ( arr [ 0 ] , 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; mp . put ( arr [ i ] , i ) ; } int len = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < K ) continue ; else { int x = K - arr [ i ] ; if ( x == 0 ) len = Math . min ( len , i ) ; if ( mp . containsValue ( x ) ) continue ; else { len = Math . min ( len , i ) ; } } } return len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 3 , 2 , 4 , 1 } ; int n = arr . length ; int K = 7 ; int len = subArraylen ( arr , n , K ) ; if ( len == Integer . MAX_VALUE ) { System . out . print ( " - 1" ) ; } else { System . out . print ( len + "NEW_LINE"); } } }
Find depth of the deepest odd level leaf node | Java 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 Code ; construct a tree
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static int maxOddLevelDepth ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int result = Integer . MAX_VALUE ; int level = 0 ; while ( ! q . isEmpty ( ) ) { int size = q . size ( ) ; level += 1 ; while ( size > 0 ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left == null && temp . right == null && ( level % 2 != 0 ) ) { result = level ; } if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } size -= 1 ; } } return result ; } public static void main ( String [ ] args ) { 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 == Integer . MAX_VALUE ) System . out . println ( " No ▁ leaf ▁ node ▁ at ▁ odd ▁ level " ) ; else { System . out . print ( result ) ; System . out . println ( " ▁ is ▁ the ▁ required ▁ depth ▁ " ) ; } } }
Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range | Java 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
import java . util . * ; class GFG { static int prime [ ] = new int [ 100001 ] ; static 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 ; } } static 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 ; } static void fibonacciOfPrime ( int n1 , int n2 ) { SieveOfEratosthenes ( ) ; Vector < Integer > initial = new Vector < > ( ) ; for ( int i = n1 ; i <= n2 ; i ++ ) if ( prime [ i ] == 1 ) initial . add ( i ) ; Vector < Integer > now = new Vector < > ( ) ; for ( int i = 0 ; i < initial . size ( ) ; i ++ ) { for ( int j = 0 ; j < initial . size ( ) ; j ++ ) { int a = ( int ) initial . get ( i ) ; int b = ( int ) initial . get ( j ) ; if ( a != b ) { int c = join ( a , b ) ; now . add ( c ) ; } } } Vector < Integer > current = new Vector < > ( ) ; for ( int i = 0 ; i < now . size ( ) ; i ++ ) if ( prime [ ( int ) now . get ( i ) ] == 1 ) current . add ( ( int ) now . get ( i ) ) ; int cnt [ ] = new int [ 1000009 ] ; for ( int i = 0 ; i < 1000001 ; i ++ ) cnt [ i ] = 0 ; Vector < Integer > current_set = new Vector < > ( ) ; for ( int i = 0 ; i < current . size ( ) ; i ++ ) { cnt [ ( int ) current . get ( i ) ] ++ ; if ( cnt [ ( int ) current . get ( i ) ] == 1 ) current_set . add ( ( int ) current . get ( i ) ) ; } long first = 1000000000 ; for ( int i = 0 ; i < current_set . size ( ) ; i ++ ) first = Math . min ( first , ( int ) current_set . get ( i ) ) ; long second = 0 ; for ( int i = 0 ; i < current_set . size ( ) ; i ++ ) second = Math . max ( second , ( int ) current_set . get ( i ) ) ; int count = current_set . size ( ) - 1 ; long curr = 1 ; long c = 0 ; while ( curr < count ) { c = first + second ; first = second ; second = c ; curr += 1 ; } System . out . println ( c ) ; } public static void main ( String [ ] args ) { 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 | Java 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
import java . util . * ; class GFG { static int countElements ( int arr [ ] , int n , int k ) { Stack < Integer > s = new Stack < Integer > ( ) ; int [ ] next_greater = new int [ n + 1 ] ; Arrays . fill ( next_greater , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . isEmpty ( ) ) { s . add ( i ) ; continue ; } while ( ! s . isEmpty ( ) && arr [ s . peek ( ) ] < arr [ i ] ) { next_greater [ s . peek ( ) ] = i ; s . pop ( ) ; } s . add ( i ) ; } int count = 0 ; int maxi = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( next_greater [ i ] - i > k && maxi < arr [ i ] ) { maxi = Math . max ( maxi , arr [ i ] ) ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 3 , 6 , 4 , 3 , 2 } ; int K = 2 ; int n = arr . length ; System . out . print ( countElements ( arr , n , K ) ) ; } }
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Java 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
class GFG { public static void maxLengthOf1s ( int arr [ ] , int n ) { int prefix [ ] = new int [ n ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i - 2 ] == 1 ) prefix [ i ] = prefix [ i - 1 ] + 1 ; else prefix [ i ] = 0 ; } int suffix [ ] = new int [ n ] ; 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 = Math . max ( ans , prefix [ i + 1 ] + suffix [ i ] ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int n = 6 ; int arr [ ] = { 1 , 1 , 1 , 0 , 1 , 1 } ; maxLengthOf1s ( arr , n ) ; } }
Minimum number of operations required to obtain a given Binary String | Java 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
import java . util . * ; class GFG { static int minOperations ( String s ) { int n = s . length ( ) ; int pos = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( 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 . charAt ( i ) == '0' ) { if ( last == 1 ) { ans ++ ; last = 0 ; } } else { if ( last == 0 ) { ans ++ ; last = 1 ; } } } return ans ; } public static void main ( String [ ] args ) { String s = "10111" ; System . out . println ( minOperations ( s ) ) ; } }
Find the Deepest Node in a Binary Tree | Java program to find value of the deepest node in a given binary tree ; A tree 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 code
class GFG { static class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = null ; right = null ; } } static int maxLevel = - 1 ; static int res = - 1 ; static void find ( Node root , int level ) { if ( root != null ) { find ( root . left , ++ level ) ; if ( level > maxLevel ) { res = root . data ; maxLevel = level ; } find ( root . right , level ) ; } } static int deepestNode ( Node root ) { int res = - 1 ; int maxLevel = - 1 ; find ( root , 0 ) ; return res ; } public static void main ( String [ ] args ) { 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 ) ; System . out . println ( deepestNode ( root ) ) ; } }
Find if string is K | Java 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
import java . util . * ; class GFG { static void iskPalindromesPossible ( String s , int k ) { if ( s . length ( ) < k ) { System . out . print ( " Not ▁ Possible " + "NEW_LINE"); return ; } if ( s . length ( ) == k ) { System . out . print ( " Possible " + "NEW_LINE"); return ; } HashMap < Character , Integer > freq = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( freq . containsKey ( s . charAt ( i ) ) ) { freq . put ( s . charAt ( i ) , freq . get ( s . charAt ( i ) ) + 1 ) ; } else { freq . put ( s . charAt ( i ) , 1 ) ; } int count = 0 ; for ( Map . Entry < Character , Integer > it : freq . entrySet ( ) ) { if ( it . getValue ( ) % 2 == 1 ) count ++ ; } if ( count > k ) System . out . print ( " No " + "NEW_LINE"); else System . out . print ( " Yes " + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " poor " ; int K = 3 ; iskPalindromesPossible ( str , K ) ; str = " geeksforgeeks " ; K = 10 ; iskPalindromesPossible ( str , K ) ; } }
Find the Deepest Node in a Binary Tree | A Java 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 Codede ; Calculating height of tree ; Printing the deepest node
class GFG { static class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = null ; right = null ; } } ; static int height ( Node root ) { if ( root == null ) return 0 ; int leftHt = height ( root . left ) ; int rightHt = height ( root . right ) ; return Math . max ( leftHt , rightHt ) + 1 ; } static void deepestNode ( Node root , int levels ) { if ( root == null ) return ; if ( levels == 1 ) System . out . print ( root . data + " ▁ " ) ; else if ( levels > 1 ) { deepestNode ( root . left , levels - 1 ) ; deepestNode ( root . right , levels - 1 ) ; } } public static void main ( String args [ ] ) { 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 ) ; } }