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 ) ;