codeparrot/xlcost-text-to-code huggingface dataset:
Submatrix Sum Queries | C # program to compute submatrix query sum in O ( 1 ) time ; Function to preprcess input mat [ M ] [ N ] . This function mainly fills aux [ M ] [ N ] such that aux [ i ] [ j ] stores sum of elements from ( 0 , 0 ) to ( i , j ) ; Copy first row of mat [ ] [ ] to aux [ ] [ ] ; Do column wise sum ; Do row wise sum ; A O ( 1 ) time function to compute sum of submatrix between ( tli , tlj ) and ( rbi , rbj ) using aux [ ] [ ] which is built by the preprocess function ; result is now sum of elements between ( 0 , 0 ) and ( rbi , rbj ) ; Remove elements between ( 0 , 0 ) and ( tli - 1 , rbj ) ; Remove elements between ( 0 , 0 ) and ( rbi , tlj - 1 ) ; Add aux [ tli - 1 ] [ tlj - 1 ] as elements between ( 0 , 0 ) and ( tli - 1 , tlj - 1 ) are subtracted twice ; Driver code
using System ; class GFG { static int M = 4 ; static int N = 5 ; static int preProcess ( int [ , ] mat , int [ , ] aux ) { for ( int i = 0 ; i < N ; i ++ ) aux [ 0 , i ] = mat [ 0 , i ] ; for ( int i = 1 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) aux [ i , j ] = mat [ i , j ] + aux [ i - 1 , j ] ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 1 ; j < N ; j ++ ) aux [ i , j ] += aux [ i , j - 1 ] ; return 0 ; } static int sumQuery ( int [ , ] aux , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi , rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 , rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi , tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 , tlj - 1 ] ; return res ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; int [ , ] aux = new int [ M , N ] ; preProcess ( mat , aux ) ; int tli = 2 , tlj = 2 , rbi = 3 , rbj = 4 ; Console . Write ( " Query1 : " sumQuery ( aux , tli , tlj , rbi , rbj ) ) ; tli = 0 ; tlj = 0 ; rbi = 1 ; rbj = 1 ; Console . Write ( " Query2 : " sumQuery ( aux , tli , tlj , rbi , rbj ) ) ; tli = 1 ; tlj = 2 ; rbi = 3 ; rbj = 3 ; Console . Write ( " Query3 : " sumQuery ( aux , tli , tlj , rbi , rbj ) ) ; } }
Program for Rank of Matrix | C # program to find rank of a matrix ; function for exchanging two rows of a matrix ; function for finding rank of matrix ; Before we visit current row ' row ' , we make sure that mat [ row ] [ 0 ] , ... . mat [ row ] [ row - 1 ] are 0. Diagonal element is not zero ; This makes all entries of current column as 0 except entry ' mat [ row ] [ row ] ' ; Diagonal element is already zero . Two cases arise : 1 ) If there is a row below it with non - zero entry , then swap this row with that row and process that row 2 ) If all elements in current column below mat [ r ] [ row ] are 0 , then remvoe this column by swapping it with last column and reducing number of columns by 1. ; Find the non - zero element in current column ; Swap the row with non - zero element with this row . ; If we did not find any row with non - zero element in current columnm , then all values in this column are 0. ; Reduce number of columns ; Copy the last column here ; Process this row again ; Uncomment these lines to see intermediate results display ( mat , R , C ) ; printf ( " \n " ) ; ; Function to display a matrix ; Driver code
using System ; class GFG { static int R = 3 ; static int C = 3 ; static void swap ( int [ , ] mat , int row1 , int row2 , int col ) { for ( int i = 0 ; i < col ; i ++ ) { int temp = mat [ row1 , i ] ; mat [ row1 , i ] = mat [ row2 , i ] ; mat [ row2 , i ] = temp ; } } static int rankOfMatrix ( int [ , ] mat ) { int rank = C ; for ( int row = 0 ; row < rank ; row ++ ) { if ( mat [ row , row ] != 0 ) { for ( int col = 0 ; col < R ; col ++ ) { if ( col != row ) { double mult = ( double ) mat [ col , row ] / mat [ row , row ] ; for ( int i = 0 ; i < rank ; i ++ ) mat [ col , i ] -= ( int ) mult * mat [ row , i ] ; } } } else { bool reduce = true ; for ( int i = row + 1 ; i < R ; i ++ ) { if ( mat [ i , row ] != 0 ) { swap ( mat , row , i , rank ) ; reduce = false ; break ; } } if ( reduce ) { rank -- ; for ( int i = 0 ; i < R ; i ++ ) mat [ i , row ] = mat [ i , rank ] ; } row -- ; } } return rank ; } static void display ( int [ , ] mat , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) Console . Write ( " ▁ " + mat [ i , j ] ) ; Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( ) { int [ , ] mat = { { 10 , 20 , 10 } , { - 20 , - 30 , 10 } , { 30 , 50 , 0 } } ; Console . Write ( " Rank ▁ of ▁ the ▁ matrix ▁ is ▁ : ▁ " + rankOfMatrix ( mat ) ) ; } }
Maximum size rectangle binary sub | C # program to find largest rectangle with all 1 s in a binary matrix ; Finds the maximum area under the histogram represented by histogram . See below article for details . https : www . geeksforgeeks . org / largest - rectangle - under - histogram / ; Create an empty stack . The stack holds indexes of hist [ ] array . The bars stored in stack are always in increasing order of their heights . ; Top of stack ; Initialize max area in ; current row ( or histogram ) Initialize area with ; current top Run through all bars of given histogram ( or row ) ; If this bar is higher than the bar on top stack , push it to stack ; If this bar is lower than top of stack , then calculate area of rectangle with stack top as the smallest ( or minimum height ) bar . ' i ' is ' right ▁ index ' for the top and element before top in stack is ' left ▁ index ' ; Now pop the remaining bars from stack and calculate area with every popped bar as the smallest bar ; Returns area of the largest rectangle with all 1 s in A [ ] [ ] ; Calculate area for first row and initialize it as result ; iterate over row to find maximum rectangular area considering each row as histogram ; if A [ i ] [ j ] is 1 then add A [ i - 1 ] [ j ] ; Update result if area with current row ( as last row of rectangle ) is more ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int maxHist ( int R , int C , int [ ] row ) { Stack < int > result = new Stack < int > ( ) ; int top_val ; int max_area = 0 ; int area = 0 ; int i = 0 ; while ( i < C ) { if ( result . Count == 0 || row [ result . Peek ( ) ] <= row [ i ] ) { result . Push ( i ++ ) ; } else { top_val = row [ result . Peek ( ) ] ; result . Pop ( ) ; area = top_val * i ; if ( result . Count > 0 ) { area = top_val * ( i - result . Peek ( ) - 1 ) ; } max_area = Math . Max ( area , max_area ) ; } } while ( result . Count > 0 ) { top_val = row [ result . Peek ( ) ] ; result . Pop ( ) ; area = top_val * i ; if ( result . Count > 0 ) { area = top_val * ( i - result . Peek ( ) - 1 ) ; } max_area = Math . Max ( area , max_area ) ; } return max_area ; } public static int maxRectangle ( int R , int C , int [ ] [ ] A ) { int result = maxHist ( R , C , A [ 0 ] ) ; for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( A [ i ] [ j ] == 1 ) { A [ i ] [ j ] += A [ i - 1 ] [ j ] ; } } result = Math . Max ( result , maxHist ( R , C , A [ i ] ) ) ; } return result ; } public static void Main ( string [ ] args ) { int R = 4 ; int C = 4 ; int [ ] [ ] A = new int [ ] [ ] { new int [ ] { 0 , 1 , 1 , 0 } , new int [ ] { 1 , 1 , 1 , 1 } , new int [ ] { 1 , 1 , 1 , 1 } , new int [ ] { 1 , 1 , 0 , 0 } } ; Console . Write ( " Area ▁ of ▁ maximum ▁ rectangle ▁ is ▁ " + maxRectangle ( R , C , A ) ) ; } }
Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | C # implementation of the approach ; A structure to represent a cell index ; r is row , varies from 0 to R - 1 ; c is column , varies from 0 to C - 1 ; Constructor ; A simple solution to find sums for a given array of cell indexes ; Iterate through all cell indexes ; Compute sum for current cell index ; Driver code
using System ; class GFG { static int R = 3 ; static int C = 3 ; public class Cell { public int r ; public int c ; public Cell ( int r , int c ) { this . r = r ; this . c = c ; } } ; static void printSums ( int [ , ] mat , Cell [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 , r = arr [ i ] . r , c = arr [ i ] . c ; for ( int j = 0 ; j < R ; j ++ ) { for ( int k = 0 ; k < C ; k ++ ) { if ( j != r && k != c ) { sum += mat [ j , k ] ; } } } Console . WriteLine ( sum ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 1 , 2 } , { 3 , 4 , 6 } , { 5 , 3 , 2 } } ; Cell [ ] arr = { new Cell ( 0 , 0 ) , new Cell ( 1 , 1 ) , new Cell ( 0 , 1 ) } ; int n = arr . Length ; printSums ( mat , arr , n ) ; } }
Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | An efficient C # program to compute sum for given array of cell indexes ; A structure to represent a cell index ; r is row , varies from 0 to R - 1 ; c is column , varies from 0 to C - 1 ; Compute sum of all elements , sum of every row and sum every column ; Compute the desired sum for all given cell indexes ; Driver Code
using System ; class GFG { static int R = 3 ; static int C = 3 ; public class Cell { public int r ; public int c ; public Cell ( int r , int c ) { this . r = r ; this . c = c ; } } ; static void printSums ( int [ , ] mat , Cell [ ] arr , int n ) { int sum = 0 ; int [ ] row = new int [ R ] ; int [ ] col = new int [ C ] ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { sum += mat [ i , j ] ; col [ j ] += mat [ i , j ] ; row [ i ] += mat [ i , j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { int ro = arr [ i ] . r , co = arr [ i ] . c ; Console . WriteLine ( sum - row [ ro ] - col [ co ] + mat [ ro , co ] ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 1 , 2 } , { 3 , 4 , 6 } , { 5 , 3 , 2 } } ; Cell [ ] arr = { new Cell ( 0 , 0 ) , new Cell ( 1 , 1 ) , new Cell ( 0 , 1 ) } ; int n = arr . Length ; printSums ( mat , arr , n ) ; } }
Count number of islands where every island is row | A C # program to count the number of rectangular islands where every island is separated by a line ; This function takes a matrix of ' X ' and ' O ' and returns the number of rectangular islands of ' X ' where no two islands are row - wise or column - wise adjacent , the islands may be diagonaly adjacent ; Initialize result ; Traverse the input matrix ; If current cell is ' X ' , then check whether this is top - leftmost of a rectangle . If yes , then increment count ; Driver program
using System ; class GFG { static int countIslands ( int [ , ] mat , int m , int n ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i , j ] == ' X ' ) { if ( ( i == 0 mat [ i - 1 , j ] == ' O ' ) && ( j == 0 mat [ i , j - 1 ] == ' O ' ) ) count ++ ; } } } return count ; } public static void Main ( ) { int m = 6 ; int n = 3 ; int [ , ] mat = { { ' O ' , ' O ' , ' O ' } , { ' X ' , ' X ' , ' O ' } , { ' X ' , ' X ' , ' O ' } , { ' O ' , ' O ' , ' X ' } , { ' O ' , ' O ' , ' X ' } , { ' X ' , ' X ' , ' O ' } } ; Console . WriteLine ( " Number ▁ of ▁ rectangular ▁ " + " islands ▁ is : ▁ " + countIslands ( mat , m , n ) ) ; } }
Find a common element in all rows of a given row | A C # program to find a common element in all rows of a row wise sorted array ; Specify number of rows and columns ; Returns common element in all rows of mat [ M ] [ N ] . If there is no common element , then - 1 is returned ; An array to store indexes of current last column ; To store index of row whose current last element is minimum ; Initialize current last element of all rows ; Initialize min_row as first row ; Keep finding min_row in current last column , till either all elements of last column become same or we hit first column . ; Find minimum in current last column ; eq_count is count of elements equal to minimum in current last column . ; Traverse current last column elements again to update it ; Decrease last column index of a row whose value is more than minimum . ; Reduce last column index by 1 ; If equal count becomes M , return the value ; Driver code
using System ; class GFG { static int M = 4 ; static int N = 5 ; static int findCommon ( int [ , ] mat ) { int [ ] column = new int [ M ] ; int min_row ; int i ; for ( i = 0 ; i < M ; i ++ ) column [ i ] = N - 1 ; min_row = 0 ; while ( column [ min_row ] >= 0 ) { for ( i = 0 ; i < M ; i ++ ) { if ( mat [ i , column [ i ] ] < mat [ min_row , column [ min_row ] ] ) min_row = i ; } int eq_count = 0 ; for ( i = 0 ; i < M ; i ++ ) { if ( mat [ i , column [ i ] ] > mat [ min_row , column [ min_row ] ] ) { if ( column [ i ] == 0 ) return - 1 ; column [ i ] -= 1 ; } else eq_count ++ ; } if ( eq_count == M ) return mat [ min_row , column [ min_row ] ] ; } return - 1 ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 } , { 2 , 4 , 5 , 8 , 10 } , { 3 , 5 , 7 , 9 , 11 } , { 1 , 3 , 5 , 7 , 9 } } ; int result = findCommon ( mat ) ; if ( result == - 1 ) Console . Write ( " No ▁ common ▁ element " ) ; else Console . Write ( " Common ▁ element ▁ is ▁ " + result ) ; } }
Find a common element in all rows of a given row | C # implementation of the approach ; Specify number of rows and columns ; Returns common element in all rows of mat [ M , N ] . If there is no common element , then - 1 is returned ; A hash map to store count of elements ; Increment the count of first element of the row ; Starting from the second element of the current row ; If current element is different from the previous element i . e . it is appearing for the first time in the current row ; Find element having count equal to number of rows ; No such element found ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int M = 4 ; static int N = 5 ; static int findCommon ( int [ , ] mat ) { Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; int i , j ; for ( i = 0 ; i < M ; i ++ ) { if ( cnt . ContainsKey ( mat [ i , 0 ] ) ) { cnt [ mat [ i , 0 ] ] = cnt [ mat [ i , 0 ] ] + 1 ; } else { cnt . Add ( mat [ i , 0 ] , 1 ) ; } for ( j = 1 ; j < N ; j ++ ) { if ( mat [ i , j ] != mat [ i , j - 1 ] ) if ( cnt . ContainsKey ( mat [ i , j ] ) ) { cnt [ mat [ i , j ] ] = cnt [ mat [ i , j ] ] + 1 ; } else { cnt . Add ( mat [ i , j ] , 1 ) ; } } } foreach ( KeyValuePair < int , int > ele in cnt ) { if ( ele . Value == M ) return ele . Key ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 } , { 2 , 4 , 5 , 8 , 10 } , { 3 , 5 , 7 , 9 , 11 } , { 1 , 3 , 5 , 7 , 9 } } ; int result = findCommon ( mat ) ; if ( result == - 1 ) Console . WriteLine ( " No ▁ common ▁ element " ) ; else Console . WriteLine ( " Common ▁ element ▁ is ▁ " + result ) ; } }
Given a matrix of ‘ O ’ and ‘ X ’ , replace ' O ' with ' X ' if surrounded by ' X ' | A C # program to replace all ' O ' s with ' X ' ' s ▁ if ▁ surrounded ▁ by ▁ ' X ' ; Size of given matrix is M X N ; A recursive function to replace previous value ' prevV ' at ' ( x , ▁ y ) ' and all surrounding values of ( x , y ) with new value ' newV ' . ; Base cases ; Replace the color at ( x , y ) ; Recur for north , east , south and west ; Returns size of maximum size subsquare matrix surrounded by ' X ' ; Step 1 : Replace all ' O ' with ' - ' ; Call floodFill for all ' - ' lying on edges Left side ; Right side ; Top side ; Bottom side ; Step 3 : Replace all ' - ' with ' X ' ; Driver Code
using System ; class GFG { static int M = 6 ; static int N = 6 ; static void floodFillUtil ( char [ , ] mat , int x , int y , char prevV , char newV ) { if ( x < 0 x >= M y < 0 y >= N ) return ; if ( mat [ x , y ] != prevV ) return ; mat [ x , y ] = newV ; floodFillUtil ( mat , x + 1 , y , prevV , newV ) ; floodFillUtil ( mat , x - 1 , y , prevV , newV ) ; floodFillUtil ( mat , x , y + 1 , prevV , newV ) ; floodFillUtil ( mat , x , y - 1 , prevV , newV ) ; } static void replaceSurrounded ( char [ , ] mat ) { for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( mat [ i , j ] == ' O ' ) mat [ i , j ] = ' - ' ; for ( int i = 0 ; i < M ; i ++ ) if ( mat [ i , 0 ] == ' - ' ) floodFillUtil ( mat , i , 0 , ' - ' , ' O ' ) ; for ( int i = 0 ; i < M ; i ++ ) if ( mat [ i , N - 1 ] == ' - ' ) floodFillUtil ( mat , i , N - 1 , ' - ' , ' O ' ) ; for ( int i = 0 ; i < N ; i ++ ) if ( mat [ 0 , i ] == ' - ' ) floodFillUtil ( mat , 0 , i , ' - ' , ' O ' ) ; for ( int i = 0 ; i < N ; i ++ ) if ( mat [ M - 1 , i ] == ' - ' ) floodFillUtil ( mat , M - 1 , i , ' - ' , ' O ' ) ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( mat [ i , j ] == ' - ' ) mat [ i , j ] = ' X ' ; } public static void Main ( ) { char [ , ] mat = new char [ , ] { { ' X ' , ' O ' , ' X ' , ' O ' , ' X ' , ' X ' } , { ' X ' , ' O ' , ' X ' , ' X ' , ' O ' , ' X ' } , { ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' X ' } , { ' O ' , ' X ' , ' X ' , ' X ' , ' X ' , ' X ' } , { ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' O ' } , { ' O ' , ' O ' , ' X ' , ' O ' , ' O ' , ' O ' } } ; replaceSurrounded ( mat ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( mat [ i , j ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } } }
Convert a given Binary Tree to Doubly Linked List | Set 2 | C # program to convert BTT to DLL using simple inorder traversal ; A tree node ; Standard Inorder traversal of tree ; Changes left pointers to work as previous pointers in converted DLL The function simply does inorder traversal of Binary Tree and updates left pointer using previously visited node ; Changes right pointers to work as next pointers in converted DLL ; Find the right most node in BT or last node in DLL ; Start from the rightmost node , traverse back using left pointers . While traversing , change right pointer of nodes ; The leftmost node is head of linked list , return it ; The main function that converts BST to DLL and returns head of DLL ; Set the previous pointer ; Set the next pointer and return head of DLL ; Traverses the DLL from left tor right ; Driver code ; Let us create the tree shown in above diagram
using System ; class GFG { public class node { public int data ; public node left , right ; public node ( int data ) { this . data = data ; } } public static node prev ; public static void inorder ( node root ) { if ( root == null ) { return ; } inorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; inorder ( root . right ) ; } public static void fixPrevptr ( node root ) { if ( root == null ) { return ; } fixPrevptr ( root . left ) ; root . left = prev ; prev = root ; fixPrevptr ( root . right ) ; } public static node fixNextptr ( node root ) { while ( root . right != null ) { root = root . right ; } while ( root != null && root . left != null ) { node left = root . left ; left . right = root ; root = root . left ; } return root ; } public static node BTTtoDLL ( node root ) { prev = null ; fixPrevptr ( root ) ; return fixNextptr ( root ) ; } public static void printlist ( node root ) { while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; root = root . right ; } } public static void Main ( ) { node root = new node ( 10 ) ; root . left = new node ( 12 ) ; root . right = new node ( 15 ) ; root . left . left = new node ( 25 ) ; root . left . right = new node ( 30 ) ; root . right . left = new node ( 36 ) ; Console . WriteLine ( " Inorder ▁ Tree ▁ Traversal " ) ; inorder ( root ) ; node head = BTTtoDLL ( root ) ; Console . WriteLine ( " STRNEWLINE DLL ▁ Traversal " ) ; printlist ( head ) ; } }
Flood fill Algorithm | C # program to implement flood fill algorithm ; Dimentions of paint screen ; A recursive function to replace previous color ' prevC ' at ' ( x , ▁ y ) ' and all surrounding pixels of ( x , y ) with new color ' newC ' and ; Base cases ; Replace the color at ( x , y ) ; Recur for north , east , south and west ; It mainly finds the previous color on ( x , y ) and calls floodFillUtil ( ) ; Driver code
using System ; class GFG { static int M = 8 ; static int N = 8 ; static void floodFillUtil ( int [ , ] screen , int x , int y , int prevC , int newC ) { if ( x < 0 x >= M y < 0 y >= N ) return ; if ( screen [ x , y ] != prevC ) return ; screen [ x , y ] = newC ; floodFillUtil ( screen , x + 1 , y , prevC , newC ) ; floodFillUtil ( screen , x - 1 , y , prevC , newC ) ; floodFillUtil ( screen , x , y + 1 , prevC , newC ) ; floodFillUtil ( screen , x , y - 1 , prevC , newC ) ; } static void floodFill ( int [ , ] screen , int x , int y , int newC ) { int prevC = screen [ x , y ] ; floodFillUtil ( screen , x , y , prevC , newC ) ; } public static void Main ( String [ ] args ) { int [ , ] screen = { { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 } , { 1 , 2 , 2 , 2 , 2 , 0 , 1 , 0 } , { 1 , 1 , 1 , 2 , 2 , 0 , 1 , 0 } , { 1 , 1 , 1 , 2 , 2 , 2 , 2 , 0 } , { 1 , 1 , 1 , 1 , 1 , 2 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 , 2 , 2 , 1 } } ; int x = 4 , y = 4 , newC = 3 ; floodFill ( screen , x , y , newC ) ; Console . WriteLine ( " Updated ▁ screen ▁ after " + " call ▁ to ▁ floodFill : ▁ " ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( screen [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } }
Convert a given Binary Tree to Doubly Linked List | Set 3 | A C # program for in - place conversion of Binary Tree to DLL ; A binary tree node has data , left pointers and right pointers ; head -- > Pointer to head node of created doubly linked list ; A simple recursive function to convert a given Binary tree to Doubly Linked List root -- > Root of Binary Tree ; Base case ; Initialize previously visited node as NULL . This is static so that the same value is accessible in all recursive calls ; Recursively convert left subtree ; Now convert this node ; Finally convert right subtree ; Function to print nodes in a given doubly linked list ; Driver Code ; Let us create the tree as shown in above diagram ; convert to DLL ; Print the converted List
using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class GFG { public Node root ; public Node head ; public virtual void BinaryTree2DoubleLinkedList ( Node root ) { if ( root == null ) { return ; } public static Node prev = null ; BinaryTree2DoubleLinkedList ( root . left ) ; if ( prev == null ) { head = root ; } else { root . left = prev ; prev . right = root ; } prev = root ; BinaryTree2DoubleLinkedList ( root . right ) ; } public virtual void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . right ; } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 15 ) ; tree . root . left . left = new Node ( 25 ) ; tree . root . left . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 36 ) ; tree . BinaryTree2DoubleLinkedList ( tree . root ) ; tree . printList ( tree . head ) ; } }