Left rotate digits of node values of all levels of a Binary Tree in increasing order | Java program for the above approach ; 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
import java . util . * ; class GFG { static class TreeNode { public int val ; public TreeNode left , right ; } ; static TreeNode newNode ( int v ) { TreeNode temp = new TreeNode ( ) ; temp . val = v ; temp . left = temp . right = null ; return temp ; } static boolean isInc ( TreeNode root ) { Queue < TreeNode > que = new LinkedList < > ( ) ; que . add ( root ) ; while ( true ) { int len = que . size ( ) ; if ( len == 0 ) break ; TreeNode pre = que . peek ( ) ; while ( len > 0 ) { TreeNode temp = que . peek ( ) ; que . poll ( ) ; if ( pre . val > temp . val ) return false ; pre = temp ; if ( temp . left != null ) que . add ( temp . left ) ; if ( temp . right != null ) que . add ( temp . right ) ; len -= 1 ; } } return true ; } static void levelOrder ( TreeNode root ) { Queue < TreeNode > que = new LinkedList < > ( ) ; que . add ( root ) ; while ( true ) { int len = que . size ( ) ; if ( len == 0 ) break ; while ( len > 0 ) { TreeNode temp = que . peek ( ) ; que . poll ( ) ; System . out . print ( temp . val + " ▁ " ) ; if ( temp . left != null ) que . add ( temp . left ) ; if ( temp . right != null ) que . add ( temp . right ) ; len -= 1 ; } System . out . println ( ) ; } System . out . println ( ) ; } static void makeInc ( TreeNode root ) { Queue < TreeNode > que = new LinkedList < > ( ) ; que . add ( root ) ; while ( true ) { int len = que . size ( ) ; if ( len == 0 ) break ; int prev = - 1 ; while ( len > 0 ) { TreeNode temp = que . peek ( ) ; que . poll ( ) ; int optEle = temp . val ; String strEle = String . valueOf ( optEle ) ; boolean flag = true ; int yy = strEle . length ( ) ; for ( int idx = 0 ; idx < strEle . length ( ) ; idx ++ ) { String s1 = strEle . substring ( idx , yy ) ; String s2 = strEle . substring ( 0 , idx ) ; String s = s1 + s2 ; int ls = Integer . valueOf ( 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 . add ( temp . left ) ; if ( temp . right != null ) que . add ( temp . right ) ; len -= 1 ; } } if ( isInc ( root ) == true ) levelOrder ( root ) ; else System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { TreeNode root = newNode ( 341 ) ; root . left = newNode ( 241 ) ; root . right = newNode ( 123 ) ; root . left . left = newNode ( 324 ) ; root . left . right = newNode ( 235 ) ; root . right . right = newNode ( 161 ) ; makeInc ( root ) ; } }