Left rotate digits of node values of all levels of a Binary Tree in increasing order | TreeNode class ; Function to check if the nodes are in increasing order or not ; Perform Level Order Traversal ; Current len of queue ; If queue is empty ; Level order traversal ; Pop element from front of the queue ; If previous value exceeds current value , return false ; Function to print the Tree after modification ; Performs level order traversal ; Calculate size of the queue ; Iterate until queue is empty ; Function to arrange node values of each level in increasing order ; Perform level order traversal ; Calculate len of queue ; If queue is empty ; Level order traversal ; Pop element from front of the queue ; Initialize the optimal element by the initial element ; Check for all left shift operations ; Left shift ; If the current shifting gives optimal solution ; Replacing initial element by the optimal element ; Push the LST ; Push the RST ; Print the result ; Driver code
class Node { constructor ( v ) { this . val = v ; this . left = this . right = null ; } } function isInc ( root ) { let que = [ ] ; que . push ( root ) ; while ( true ) { let len = que . length ; if ( len == 0 ) break ; let pre = que [ 0 ] ; while ( len > 0 ) { let temp = que [ 0 ] ; que . shift ( ) ; if ( pre . val > temp . val ) return false ; pre = temp ; if ( temp . left != null ) que . push ( temp . left ) ; if ( temp . right != null ) que . push ( temp . right ) ; len -= 1 ; } } return true ; } function levelOrder ( root ) { let que = [ ] ; que . push ( root ) ; while ( true ) { let len = que . length ; if ( len == 0 ) break ; while ( len > 0 ) { let temp = que [ 0 ] ; que . shift ( ) ; document . write ( temp . val + " " ) ; if ( temp . left != null ) que . push ( temp . left ) ; if ( temp . right != null ) que . push ( temp . right ) ; len -= 1 ; } document . write ( " " ) ; } document . write ( " " ) ; } function makeInc ( root ) { let que = [ ] ; que . push ( root ) ; while ( true ) { let len = que . length ; if ( len == 0 ) break ; let prev = - 1 ; while ( len > 0 ) { let temp = que [ 0 ] ; que . shift ( ) ; let optEle = temp . val ; let strEle = ( optEle ) . toString ( ) ; let flag = true ; let yy = strEle . length ; for ( let idx = 0 ; idx < strEle . length ; idx ++ ) { let s1 = strEle . substring ( idx , yy ) ; let s2 = strEle . substring ( 0 , idx ) ; let s = s1 + s2 ; let ls = parseInt ( s ) ; if ( ls >= prev && flag ) { optEle = ls ; flag = false ; } if ( ls >= prev ) optEle = Math . min ( optEle , ls ) ; } temp . val = optEle ; prev = temp . val ; if ( temp . left != null ) que . push ( temp . left ) ; if ( temp . right != null ) que . push ( temp . right ) ; len -= 1 ; } } if ( isInc ( root ) == true ) levelOrder ( root ) ; else document . write ( - 1 ) ; } let root = new Node ( 341 ) ; root . left = new Node ( 241 ) ; root . right = new Node ( 123 ) ; root . left . left = new Node ( 324 ) ; root . left . right = new Node ( 235 ) ; root . right . right = new Node ( 161 ) ; makeInc ( root ) ;