codeparrot/xlcost-text-to-code huggingface dataset:
Ways to arrange Balls such that adjacent balls are of different types | javascript program to count number of ways to arrange three types of balls such that no two balls of same color are adjacent to each other ; table to store to store results of subproblems ; Returns count of arrangements where last placed ball is ' last ' . ' last ' is 0 for ' p ' , 1 for ' q ' and 2 for ' r ' ; if number of balls of any color becomes less than 0 the number of ways arrangements is 0. ; If last ball required is of type P and the number of balls of P type is 1 while number of balls of other color is 0 the number of ways is 1. ; Same case as above for ' q ' and ' r ' ; If this subproblem is already evaluated ; if last ball required is P and the number of ways is the sum of number of ways to form sequence with ' p - 1' P balls , q Q balss and r R balls ending with Q and R . ; Same as above case for ' q ' and ' r ' ; ( last == 2 ) ; Returns count of required arrangements ; Three cases arise : return countWays ( p , q , r , 0 ) + Last required balls is type P countWays ( p , q , r , 1 ) + Last required balls is type Q countWays ( p , q , r , 2 ) ; Last required balls is type R ; Driver code
var MAX = 100 ; var dp = Array ( MAX ) . fill ( - 1 ) . map ( x => Array ( MAX ) . fill ( - 1 ) . map ( x => Array ( MAX ) . fill ( - 1 ) . map ( x => Array ( 3 ) . fill ( - 1 ) ) ) ) ; function countWays ( p , q , r , last ) { if ( p < 0 q < 0 r < 0 ) return 0 ; if ( p == 1 && q == 0 && r == 0 && last == 0 ) return 1 ; if ( p == 0 && q == 1 && r == 0 && last == 1 ) return 1 ; if ( p == 0 && q == 0 && r == 1 && last == 2 ) return 1 ; if ( dp [ p ] [ q ] [ r ] [ last ] != - 1 ) return dp [ p ] [ q ] [ r ] [ last ] ; if ( last == 0 ) dp [ p ] [ q ] [ r ] [ last ] = countWays ( p - 1 , q , r , 1 ) + countWays ( p - 1 , q , r , 2 ) ; else if ( last == 1 ) dp [ p ] [ q ] [ r ] [ last ] = countWays ( p , q - 1 , r , 0 ) + countWays ( p , q - 1 , r , 2 ) ; else dp [ p ] [ q ] [ r ] [ last ] = countWays ( p , q , r - 1 , 0 ) + countWays ( p , q , r - 1 , 1 ) ; return dp [ p ] [ q ] [ r ] [ last ] ; } function countUtil ( p , q , r ) { } var p = 1 , q = 1 , r = 1 ; document . write ( countUtil ( p , q , r ) ) ;
Count Derangements ( Permutation such that no element appears in its original position ) | Function to count derangements ; Base cases ; countDer ( n ) = ( n - 1 ) [ countDer ( n - 1 ) + der ( n - 2 ) ] ; Driver Code
function countDer ( n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; return ( n - 1 ) * ( countDer ( n - 1 ) + countDer ( n - 2 ) ) ; } let n = 4 ; document . write ( " " + countDer ( n ) ) ;
Count Derangements ( Permutation such that no element appears in its original position ) | Function to count derangements ; Create an array to store counts for subproblems ; Base cases ; Fill der [ 0. . n ] in bottom up manner using above recursive formula ; Return result for n ; Driver code
function countDer ( n ) { let der = new Array ( n + 1 ) ; der [ 1 ] = 0 ; der [ 2 ] = 1 ; for ( let i = 3 ; i <= n ; ++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ; return der [ n ] ; } let n = 4 ; document . write ( " " + countDer ( n ) ) ;
Find number of solutions of a linear equation of n variables | Returns counr of solutions for given rhs and coefficients coeff [ 0. . n - 1 ] ; Create and initialize a table to store results of subproblems ; Fill table in bottom up manner ; Driver code
function countSol ( coeff , n , rhs ) { let dp = new Array ( rhs + 1 ) ; dp . fill ( 0 ) ; dp [ 0 ] = 1 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = coeff [ i ] ; j <= rhs ; j ++ ) dp [ j ] += dp [ j - coeff [ i ] ] ; return dp [ rhs ] ; } let coeff = [ 2 , 2 , 5 ] ; let rhs = 4 ; let n = coeff . length ; document . write ( countSol ( coeff , n , rhs ) ) ;
Populate Inorder Successor for all nodes | A binary tree node ; Set next of p and all descendants of p by traversing them in reverse Inorder ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in left subtree ; Constructed binary tree is 10 / \ 8 12 / 3 ; Populates nextRight pointer in all nodes ; Let us see the populated values ; - 1 is printed if there is no successor
class Node { constructor ( x ) { this . data = x ; this . left = null ; this . right = null ; } } let root ; let next = null ; function populateNext ( node ) { if ( node != null ) { populateNext ( node . right ) ; node . next = next ; next = node ; populateNext ( node . left ) ; } } root = new Node ( 10 ) root . left = new Node ( 8 ) root . right = new Node ( 12 ) root . left . left = new Node ( 3 ) p = populateNext ( root ) ptr = root . left . left while ( ptr != null ) { let print = ptr . next != null ? ptr . next . data : - 1 ; document . write ( " " + ptr . data + " " + print + " " ) ; ptr = ptr . next ; }
Maximum Product Cutting | DP | The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver program to test above functions
function maxProd ( n ) { if ( n == 2 n == 3 ) { return ( n - 1 ) ; } let res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; } document . write ( " " + maxProd ( 10 ) ) ;
Dice Throw | DP | The main function that returns the number of ways to get sum ' x ' with ' n ' dice and ' m ' with m faces . ; Create a table to store the results of subproblems . One extra row and column are used for simplicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for only one dice ; Fill rest of the entries in table using recursive relation i : number of dice , j : sum ; Return value ; Driver Code
function findWays ( m , n , x ) { let table = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { table [ i ] = new Array ( x + 1 ) ; for ( let j = 0 ; j < ( x + 1 ) ; j ++ ) { table [ i ] [ j ] = 0 ; } } for ( let j = 1 ; j <= m && j <= x ; j ++ ) table [ 1 ] [ j ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= x ; j ++ ) { for ( let k = 1 ; k < j && k <= m ; k ++ ) table [ i ] [ j ] += table [ i - 1 ] [ j - k ] ; } } return table [ n ] [ x ] ; } document . write ( findWays ( 4 , 2 , 1 ) + " " ) ; document . write ( findWays ( 2 , 2 , 3 ) + " " ) ; document . write ( findWays ( 6 , 3 , 8 ) + " " ) ; document . write ( findWays ( 4 , 2 , 5 ) + " " ) ; document . write ( findWays ( 4 , 3 , 5 ) + " " ) ;
Dice Throw | DP | * * Count ways * * @ param f * @ param d * @ param s * @ return ; Create a table to store results of subproblems . One extra row and column are used for simpilicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for no dices If you do not have any data , then the value must be 0 , so the result is 1 ; Iterate over dices ; Iterate over sum ; The result is obtained in two ways , pin the current dice and spending 1 of the value , so we have mem [ i - 1 ] [ j - 1 ] remaining combinations , to find the remaining combinations we would have to pin the values ? ? above 1 then we use mem [ i ] [ j - 1 ] to sum all combinations that pin the remaining j - 1 's. But there  is a way, when "j-f-1> = 0" we would be  adding extra combinations, so we remove  the combinations that only pin the  extrapolated dice face and subtract the  extrapolated combinations. ; Driver code
function findWays ( f , d , s ) { let mem = new Array ( d + 1 ) ; for ( let i = 0 ; i < ( d + 1 ) ; i ++ ) { mem [ i ] = new Array ( s + 1 ) ; for ( let j = 0 ; j < s + 1 ; j ++ ) { mem [ i ] [ j ] = 0 ; } } mem [ 0 ] [ 0 ] = 1 ; for ( let i = 1 ; i <= d ; i ++ ) { for ( let j = i ; j <= s ; j ++ ) { mem [ i ] [ j ] = mem [ i ] [ j - 1 ] + mem [ i - 1 ] [ j - 1 ] ; if ( j - f - 1 >= 0 ) mem [ i ] [ j ] -= mem [ i - 1 ] [ j - f - 1 ] ; } } return mem [ d ] [ s ] ; } document . write ( findWays ( 4 , 2 , 1 ) + " " ) ; document . write ( findWays ( 2 , 2 , 3 ) + " " ) ; document . write ( findWays ( 6 , 3 , 8 ) + " " ) ; document . write ( findWays ( 4 , 2 , 5 ) + " " ) ; document . write ( findWays ( 4 , 3 , 5 ) + " " ) ;
Minimum insertions to form a palindrome | DP | Recursive function to find minimum number of insertions ; Base Cases ; Check if the first and last characters are same . On the basis of the comparison result , decide which subrpoblem ( s ) to call ; Driver program to test above functions
function findMinInsertions ( str , l , h ) { if ( l > h ) return Number . MAX_VALUE ; if ( l == h ) return 0 ; if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ; return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( Math . min ( findMinInsertions ( str , l , h - 1 ) , findMinInsertions ( str , l + 1 , h ) ) + 1 ) } let str = " " ; document . write ( findMinInsertions ( str , 0 , str . length - 1 ) ) ;
Longest Palindromic Subsequence | DP | A utility function to get max of two integers ; Returns the length of the longest palindromic subsequence in seq ; Base Case 1 : If there is only 1 character ; Base Case 2 : If there are only 2 characters and both are same ; If the first and last characters match ; If the first and last characters do not match ; Driver program to test above function
function max ( x , y ) { return ( x > y ) ? x : y ; } function lps ( seq , i , j ) { if ( i == j ) { return 1 ; } if ( seq [ i ] == seq [ j ] && i + 1 == j ) { return 2 ; } if ( seq [ i ] == seq [ j ] ) { return lps ( seq , i + 1 , j - 1 ) + 2 ; } return max ( lps ( seq , i , j - 1 ) , lps ( seq , i + 1 , j ) ) ; } let seq = " " ; let n = seq . length ; document . write ( " " , lps ( seq . split ( " " ) , 0 , n - 1 ) ) ;
Count occurrences of a string that can be constructed from another given string | Function to find the count ; Initialize hash for both strings ; hash the frequency of letters of str1 ; hash the frequency of letters of str2 ; Find the count of str2 constructed from str1 ; Return answer ; Driver code
function findCount ( str1 , str2 ) { var len = str1 . length ; var len2 = str2 . length ; var ans = 21474836473 ; var hash1 = new Array ( 26 ) . fill ( 0 ) ; var hash2 = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < len ; i ++ ) hash1 [ str1 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < len2 ; i ++ ) hash2 [ str2 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < 26 ; i ++ ) if ( hash2 [ i ] ) ans = Math . min ( ans , hash1 [ i ] / hash2 [ i ] ) ; return ans ; } var str1 = " " ; var str2 = " " ; document . write ( findCount ( str1 , str2 ) ) ;
Check if a string is the typed name of the given name | Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed ; Driver code
function isVowel ( c ) { let vowel = " " ; for ( let i = 0 ; i < vowel . length ; ++ i ) { if ( vowel [ i ] == c ) { return true ; } } return false ; } function printRLE ( str , typed ) { let n = str . length , m = typed . length ; let j = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] != typed [ j ] ) { return false ; } if ( isVowel ( str [ i ] ) == false ) { j ++ ; continue ; } let count1 = 1 ; while ( i < n - 1 && str [ i ] == str [ i + 1 ] ) { count1 ++ ; i ++ ; } let count2 = 1 ; while ( j < m - 1 && typed [ j ] == str [ i ] ) { count2 ++ ; j ++ ; } if ( count1 > count2 ) { return false ; } } return true ; } let name = " " , typed = " " ; if ( printRLE ( name , typed ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Program to replace a word with asterisks in a sentence | Function takes two parameter ; Break down sentence by ' ▁ ' spaces and store each individual word in a different list ; A new string to store the result ; Creating the censor which is an asterisks " * " text of the length of censor word ; Iterating through our list of extracted words ; changing the censored word to created asterisks censor ; join the words ; Driver code
function censor ( text , word ) { var word_list = text . split ( " " ) ; var result = " " ; var stars = " " ; for ( var i = 0 ; i < word . length ; i ++ ) stars += " " ; var index = 0 ; for ( const i of word_list ) { if ( i === word ) word_list [ index ] = stars ; index ++ ; } for ( const i of word_list ) { result += i + " " ; } return result ; } var extract = " " + " " + " " ; var cen = " " ; document . write ( censor ( extract , cen ) + " " ) ;
Finite Automata algorithm for Pattern Searching | Javascript program for Finite Automata Pattern searching Algorithm ; If the character c is same as next character in pattern , then simply increment state ; ns stores the result which is next state ; Start from the largest possible value and stop when you find a prefix which is also suffix ; This function builds the TF table which represents Finite Automata for a given pattern ; Prints all occurrences of pat in txt ; Process txt over FA . ; Driver code
let NO_OF_CHARS = 256 ; function getNextState ( pat , M , state , x ) { if ( state < M && x == pat [ state ] . charCodeAt ( 0 ) ) return state + 1 ; let ns , i ; for ( ns = state ; ns > 0 ; ns -- ) { if ( pat [ ns - 1 ] . charCodeAt ( 0 ) == x ) { for ( i = 0 ; i < ns - 1 ; i ++ ) if ( pat [ i ] != pat [ state - ns + 1 + i ] ) break ; if ( i == ns - 1 ) return ns ; } } return 0 ; } function computeTF ( pat , M , TF ) { let state , x ; for ( state = 0 ; state <= M ; ++ state ) for ( x = 0 ; x < NO_OF_CHARS ; ++ x ) TF [ state ] [ x ] = getNextState ( pat , M , state , x ) ; } function search ( pat , txt ) { let M = pat . length ; let N = txt . length ; let TF = new Array ( M + 1 ) ; for ( let i = 0 ; i < M + 1 ; i ++ ) { TF [ i ] = new Array ( NO_OF_CHARS ) ; for ( let j = 0 ; j < NO_OF_CHARS ; j ++ ) TF [ i ] [ j ] = 0 ; } computeTF ( pat , M , TF ) ; let i , state = 0 ; for ( i = 0 ; i < N ; i ++ ) { state = TF [ state ] [ txt [ i ] . charCodeAt ( 0 ) ] ; if ( state == M ) document . write ( " " + " " + ( i - M + 1 ) + " " ) ; } } let pat = " " . split ( " " ) ; let txt = " " . split ( " " ) ; search ( txt , pat ) ;
Longest Substring containing '1' | Function to find length of longest substring containing '1' ; Count the number of contiguous 1 's ; Driver Code
function maxlength ( s ) { let n = s . length , i , j ; let ans = 0 ; for ( i = 0 ; i <= n - 1 ; i ++ ) { if ( s [ i ] == ' ' ) { let count = 1 ; for ( j = i + 1 ; j <= n - 1 && s [ j ] == ' ' ; j ++ ) count ++ ; ans = Math . max ( ans , count ) ; } } return ans ; } let s = " " ; document . write ( maxlength ( s ) ) ;
Generate all possible strings formed by replacing letters with given respective symbols | Function to generate all possible string by replacing the characters with mapped symbols ; Base Case ; Function call with the P - th character not replaced ; Replace the P - th character ; Function call with the P - th character replaced ; Driver Code ; Function Call
function generateLetters ( S , P , M ) { if ( P == S . length ) { document . write ( S + " " ) ; return ; } generateLetters ( S , P + 1 , M ) ; S = S . replace ( S . charAt ( P ) , M . get ( S [ P ] ) ) generateLetters ( S , P + 1 , M ) ; return ; } let S = " " ; let M = new Map ( ) ; M . set ( ' ' , ' ' ) ; M . set ( ' ' , ' ' ) ; M . set ( ' ' , ' ' ) ; M . set ( ' ' , ' ' ) ; M . set ( ' ' , ' ' ) ; M . set ( ' ' , ' ' ) ; M . set ( ' ' , ' ' ) ; generateLetters ( S , 0 , M ) ;
Max count of N using digits of M such that 2 and 5 , and , 6 and 9 can be treated as same respectively | Function to find the count of numbers that can be formed using the given digits in the string ; Store the frequency of digits from the given string M ; Store length of the string M ; Loop to traverse the string ; Replace 5 with 2 ; Replace 9 with 6 ; Get the int form of the current character in the string ; Insert in the map ; Store all the digits of the required number N ; Loop to get all the digits from the number N ; Get the last digit as the remainder ; Replace 5 with 2 ; Replace 9 with 6 ; Insert the remainders in the rems map ; Store the resultant count ; Iterate through the rems map ; Get the key which is a digit from the number N to be formed ; If not present in the string M , number N that cannot be formed ; Divide the frequency of the digit from the string M with the frequency of the current remainder ; Choose the minimum ; Return the maximum count ; Driver Code
function solve ( n , str ) { let mymap = new Map ( ) ; let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { let c = str . charAt ( i ) ; if ( c == " " ) c = " " ; else if ( c == " " ) c = " " ; let c_int = parseInt ( c ) ; if ( mymap . has ( c_int ) ) mymap . set ( c_int , mymap . get ( c_int ) + 1 ) ; else mymap . set ( c_int , 1 ) ; } let rems = new Map ( ) ; while ( n > 0 ) { let rem = n % 10 ; if ( rem == 5 ) rem = 2 ; if ( rem == 9 ) rem = 6 ; if ( rems . has ( rem ) ) rems . set ( rem , rems . get ( rem ) + 1 ) ; else rems . set ( rem , 1 ) ; n = Math . floor ( n / 10 ) ; } let cnt = Number . MAX_SAFE_INTEGER ; for ( let ele of rems ) { let key = ele [ 0 ] ; if ( ! mymap . has ( key ) ) return 0 ; let temp = mymap . get ( key ) / ele [ 1 ] ; cnt = Math . min ( cnt , temp ) ; } return cnt ; } let N = 56 ; let M = " " ; document . write ( solve ( N , M ) ) ;
Minimize swaps of pairs of characters required such that no two adjacent characters in the string are same | javascript program for the above approach ; Function to check if S contains any pair of adjacent characters that are same ; Traverse the String S ; If current pair of adjacent characters are the same ; Return true ; Utility function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Check if the required String is formed already ; Traverse the String S ; Swap the characters at i and j position ; Swap for Backtracking Step ; Function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Stores the resultant minimum number of swaps required ; Function call to find the minimum swaps required ; Prvar the result ; Driver Code
var ansSwaps ; function check ( S ) { for ( var i = 1 ; i < S . length ; i ++ ) { if ( S [ i - 1 ] == S [ i ] ) { return false ; } } return true ; } function minimumSwaps ( S , swaps , idx ) { if ( check ( S ) ) { ansSwaps = Math . min ( ansSwaps , swaps ) ; } for ( var i = idx ; i < S . length ; i ++ ) { for ( var j = i + 1 ; j < S . length ; j ++ ) { swap ( S , i , j ) ; minimumSwaps ( S , swaps + 1 , i + 1 ) ; S = swap ( S , i , j ) ; } } } function swap ( arr , i , j ) { var temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } function findMinimumSwaps ( S ) { ansSwaps = Number . MAX_VALUE ; minimumSwaps ( S , 0 , 0 ) ; if ( ansSwaps == Number . MAX_VALUE ) document . write ( " " ) ; else document . write ( ansSwaps ) ; } var S = " " ; findMinimumSwaps ( S . split ( ' ' ) ) ;
Maximum count of 0 s between two 1 s in given range for Q queries | Function to count the number of 0 s lying between the two 1 s for each query ; Stores count of 0 ' s ▁ that ▁ are ▁ ▁ right ▁ to ▁ the ▁ most ▁ recent ▁ 1' s ; Stores count of 0 ' s ▁ that ▁ are ▁ ▁ left ▁ to ▁ the ▁ most ▁ recent ▁ 1' s ; Stores the count of zeros in a prefix / suffix of array ; Stores the count of total 0 s ; Traverse the string S ; If current character is '1' ; Otherwise ; Update the rightBound [ i ] ; Update count and total to 0 ; Traverse the string S in reverse manner ; If current character is '1' ; Otherwise ; Update the leftBound [ i ] ; Traverse given query array ; Update the value of count ; Print the count as the result to the current query [ L , R ] ; Driver Code
function countOsBetween1s ( S , N , Q ) { let leftBound = new Array ( N ) ; let rightBound = new Array ( N ) ; let count = 0 ; let total = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { count = total ; } else if ( S [ i ] == ' ' ) { total ++ ; } rightBound [ i ] = count ; } count = 0 ; total = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == ' ' ) { count = total ; } else if ( S [ i ] == ' ' ) { total ++ ; } leftBound [ i ] = count ; } for ( let q = 0 ; q < 2 ; q ++ ) { let L = Q [ q ] [ 0 ] ; let R = Q [ q ] [ 1 ] ; count = leftBound [ L ] + rightBound [ R ] - total ; document . write ( count + " " ) ; } } let S = " " ; let Q = [ [ 0 , 4 ] , [ 0 , 5 ] ] ; let N = S . length ; countOsBetween1s ( S , N , Q ) ;
Lexicographically smallest Palindromic Path in a Binary Tree | Javascript program for the above approach ; Struct binary tree node ; Function to create a new node ; Function to check if the String is palindrome or not ; Function to find the lexicographically smallest palindromic path in the Binary Tree ; Base case ; Append current node 's  data to the String ; Check if a node is leaf or not ; Check for the 1 st Palindromic Path ; Store lexicographically the smallest palindromic path ; Recursively traverse left subtree ; Recursively traverse right subtree ; Function to get smallest lexographical palindromic path ; Variable which stores the final result ; Function call to compute lexicographically smallest palindromic Path ; Conbinary tree
let finalAns = " " ; class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let temp = new Node ( data ) ; return temp ; } function checkPalindrome ( s ) { let low = 0 , high = s . length - 1 ; while ( low < high ) { if ( s [ low ] != s [ high ] ) return false ; low ++ ; high -- ; } return true ; } function lexicographicallySmall ( root , s ) { if ( root == null ) return ; s += root . data ; if ( root . left == null && root . right == null ) { if ( checkPalindrome ( s ) ) { if ( finalAns == " " ) finalAns = s ; else finalAns = finalAns . localeCompare ( s ) <= 0 ? finalAns : s ; } return ; } lexicographicallySmall ( root . left , s ) ; lexicographicallySmall ( root . right , s ) ; } function getPalindromePath ( root ) { finalAns = " " ; lexicographicallySmall ( root , " " ) ; if ( finalAns == " " ) document . write ( " " ) ; else document . write ( finalAns ) ; } let root = newNode ( ' ' ) ; root . left = newNode ( ' ' ) ; root . left . left = newNode ( ' ' ) ; root . left . right = newNode ( ' ' ) ; root . right = newNode ( ' ' ) ; root . right . left = newNode ( ' ' ) ; root . right . right = newNode ( ' ' ) ; root . right . left . right = newNode ( ' ' ) ; getPalindromePath ( root ) ;