Left rotate digits of node values of all levels of a Binary Tree in increasing order | C # 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
using System ; using System . Collections . Generic ; class GFG { 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 bool isInc ( TreeNode root ) { Queue < TreeNode > que = new Queue < TreeNode > ( ) ; que . Enqueue ( root ) ; while ( true ) { int len = que . Count ; if ( len == 0 ) break ; TreeNode pre = que . Peek ( ) ; while ( len > 0 ) { TreeNode temp = que . Peek ( ) ; que . Dequeue ( ) ; if ( pre . val > temp . val ) return false ; pre = temp ; if ( temp . left != null ) que . Enqueue ( temp . left ) ; if ( temp . right != null ) que . Enqueue ( temp . right ) ; len -= 1 ; } } return true ; } static void levelOrder ( TreeNode root ) { Queue < TreeNode > que = new Queue < TreeNode > ( ) ; que . Enqueue ( root ) ; while ( true ) { int len = que . Count ; if ( len == 0 ) break ; while ( len > 0 ) { TreeNode temp = que . Peek ( ) ; que . Dequeue ( ) ; Console . Write ( temp . val + " ▁ " ) ; if ( temp . left != null ) que . Enqueue ( temp . left ) ; if ( temp . right != null ) que . Enqueue ( temp . right ) ; len -= 1 ; } Console . Write ( " STRNEWLINE " ) ; } Console . Write ( " STRNEWLINE " ) ; } static void makeInc ( TreeNode root ) { Queue < TreeNode > que = new Queue < TreeNode > ( ) ; que . Enqueue ( root ) ; while ( true ) { int len = que . Count ; if ( len == 0 ) break ; int prev = - 1 ; while ( len > 0 ) { TreeNode temp = que . Peek ( ) ; que . Dequeue ( ) ; int optEle = temp . val ; string strEle = optEle . ToString ( ) ; bool flag = true ; int yy = strEle . Length ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { string s1 = strEle . Substring ( idx , yy - idx ) ; string s2 = strEle . Substring ( 0 , idx ) ; string s = String . Concat ( s1 , s2 ) ; int ls = Int32 . Parse ( 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 . Enqueue ( temp . left ) ; if ( temp . right != null ) que . Enqueue ( temp . right ) ; len -= 1 ; } } if ( isInc ( root ) == true ) levelOrder ( root ) ; else Console . Write ( - 1 ) ; } public static void Main ( ) { 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 ) ; } }