codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find minimum swaps required to sort given array with updates | C # program for the above approach ; Function to return the position of the given value using binary search ; Return 0 if every value is greater than the given value ; Return N - 1 if every value is smaller than the given value ; Perform Binary Search ; Iterate till start < end ; Find the mid ; Update start and end ; Return the position of the given value ; Function to return the number of make the array sorted ; Index x to update ; Increment value by y ; Set newElement equals to x + y ; Compute the new index ; Print the minimum number of swaps ; Driver Code ; Given array [ ] arr ; Given Queries ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int computePos ( int [ ] arr , int n , int value ) { if ( value < arr [ 0 ] ) return 0 ; if ( value > arr [ n - 1 ] ) return n - 1 ; int start = 0 ; int end = n - 1 ; while ( start < end ) { int mid = ( start + end + 1 ) / 2 ; if ( arr [ mid ] >= value ) end = mid - 1 ; else start = mid ; } return start ; } static void countShift ( int [ ] arr , int n , List < List < int > > queries ) { foreach ( List < int > q in queries ) { int index = q [ 0 ] ; int update = q [ 1 ] ; int newElement = arr [ index ] + update ; int newIndex = computePos ( arr , n , newElement ) ; Console . Write ( Math . Abs ( newIndex - index ) + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int N = arr . Length ; List < List < int > > queries = new List < List < int > > ( ) ; List < int > v = new List < int > ( ) ; List < int > v1 = new List < int > ( ) ; v . Add ( 0 ) ; v . Add ( - 1 ) ; queries . Add ( v ) ; v1 . Add ( 4 ) ; v1 . Add ( - 11 ) ; queries . Add ( v1 ) ; countShift ( arr , N , queries ) ; } }
Count nodes having smallest value in the path from root to itself in a Binary Tree | C # program for the above approach ; Structure of a tree node ; Function to create new tree node ; Function to find the total number of required nodes ; If current node is null then return to the parent node ; Check if current node value is less than or equal to minNodeVal ; Update the value of minNodeVal ; Update the count ; Go to the left subtree ; Go to the right subtree ; Driver Code ; Binary Tree creation 8 / \ / \ 6 5 / \ / \ / \ / \ 6 7 3 9 ; Function Call ; Print the result
using System ; class GFG { public class Node { public int key ; public Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return temp ; } static int ans ; static void countReqNodes ( Node root , int minNodeVal ) { if ( root == null ) return ; if ( root . key <= minNodeVal ) { minNodeVal = root . key ; ans ++ ; } countReqNodes ( root . left , minNodeVal ) ; countReqNodes ( root . right , minNodeVal ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 8 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 6 ) ; root . left . right = newNode ( 7 ) ; root . right . left = newNode ( 3 ) ; root . right . right = newNode ( 9 ) ; int minNodeVal = int . MaxValue ; ans = 0 ; countReqNodes ( root , minNodeVal ) ; Console . Write ( ans ) ; } }
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | C # program for the above approach ; Function to check if a string is palindrome or not ; Start and end of the string ; Iterate the string until i > j ; If there is a mismatch ; Increment first pointer and decrement the other ; Given string is a palindrome ; Function two check if the strings can be combined to form a palindrome ; Initialize array of characters ; Stores character of string in the character array ; Find left and right parts of strings a and b ; Substring a [ j ... i - 1 ] ; Substring b [ j ... i - 1 ] ; Substring a [ i ... n ] ; Substring b [ i ... n ] ; Check is left part of a + right part of b or vice versa is a palindrome ; Print the result ; Otherwise ; Driver Code
using System ; using System . Text ; class GFG { static bool isPalindrome ( String str ) { int i = 0 , j = str . Length - 1 ; while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } static void formPalindrome ( String a , String b , int n ) { char [ ] aa = new char [ n + 2 ] ; char [ ] bb = new char [ n + 2 ] ; for ( int i = 0 ; i < n + 2 ; i ++ ) { aa [ i ] = ' ▁ ' ; bb [ i ] = ' ▁ ' ; } for ( int i = 1 ; i <= n ; i ++ ) { aa [ i ] = a [ i - 1 ] ; bb [ i ] = b [ i - 1 ] ; } bool ok = false ; for ( int i = 0 ; i <= n + 1 ; i ++ ) { StringBuilder la = new StringBuilder ( ) ; StringBuilder ra = new StringBuilder ( ) ; StringBuilder lb = new StringBuilder ( ) ; StringBuilder rb = new StringBuilder ( ) ; for ( int j = 1 ; j <= i - 1 ; j ++ ) { la . Append ( ( aa [ j ] == ' ▁ ' ) ? ' ▁ ' : aa [ j ] ) ; lb . Append ( ( bb [ j ] == ' ▁ ' ) ? ' ▁ ' : bb [ j ] ) ; } for ( int j = i ; j <= n + 1 ; j ++ ) { ra . Append ( ( aa [ j ] == ' ▁ ' ) ? ' ▁ ' : aa [ j ] ) ; rb . Append ( ( bb [ j ] == ' ▁ ' ) ? ' ▁ ' : bb [ j ] ) ; } if ( isPalindrome ( la . ToString ( ) + rb . ToString ( ) ) || isPalindrome ( lb . ToString ( ) + ra . ToString ( ) ) ) { ok = true ; break ; } } if ( ! ok ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( String [ ] args ) { String A = " bdea " ; String B = " abbb " ; int N = 4 ; formPalindrome ( A , B , N ) ; } }
Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2 | C # program for the above approach ; Function to return minimum element to be added in array B so that array A become subsequence of array B ; Stores indices of the array elements ; Iterate over the array ; Store the indices of the array elements ; Stores the LIS ; Check if element B [ i ] is in array [ ] A ; Perform Binary Search ; Find the value of mid m ; Update l and r ; If found better element ' e ' for pos r + 1 ; Otherwise , extend the current subsequence ; Return the answer ; Driver Code ; Given arrays ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int minElements ( int [ ] A , int [ ] B , int N , int M ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < A . Length ; i ++ ) { map . Add ( A [ i ] , i ) ; } List < int > subseq = new List < int > ( ) ; int l = 0 , r = - 1 ; for ( int i = 0 ; i < M ; i ++ ) { if ( map . ContainsKey ( B [ i ] ) ) { int e = map [ B [ i ] ] ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( subseq [ m ] < e ) l = m + 1 ; else r = m - 1 ; } if ( r + 1 < subseq . Count ) { subseq [ r + 1 ] = e ; } else { subseq . Add ( e ) ; } l = 0 ; r = subseq . Count - 1 ; } } return N - subseq . Count ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; int [ ] B = { 2 , 5 , 6 , 4 , 9 , 12 } ; int M = A . Length ; int N = B . Length ; Console . WriteLine ( minElements ( A , B , M , N ) ) ; } }
Detect a negative cycle in a Graph using Shortest Path Faster Algorithm | C # program for the above approach ; Stores the adjacency list of the given graph ; Create Adjacency List ; Stores the distance of all reachable vertex from source ; Check if vertex is present in queue or not ; Counts the relaxation for each vertex ; Distance from src to src is 0 ; Create a queue ; Push source in the queue ; Mark source as visited ; Front vertex of Queue ; Relaxing all edges of vertex from the Queue ; Update the dist [ v ] to minimum distance ; If vertex v is in Queue ; Negative cycle ; No cycle found ; Driver Code ; Number of vertices ; Given source node src ; Number of Edges ; Given Edges with weight ; If cycle is present
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static bool sfpa ( int V , int src , int [ , ] Edges , int M ) { List < pair > [ ] g = new List < pair > [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { g [ i ] = new List < pair > ( ) ; } for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; int w = Edges [ i , 2 ] ; g [ u ] . Add ( new pair ( v , w ) ) ; } int [ ] dist = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) dist [ i ] = int . MaxValue ; bool [ ] inQueue = new bool [ V ] ; int [ ] cnt = new int [ V ] ; dist [ src ] = 0 ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( src ) ; inQueue [ src ] = true ; while ( q . Count != 0 ) { int u = q . Peek ( ) ; q . Dequeue ( ) ; inQueue [ u ] = false ; foreach ( pair x in g [ u ] ) { int v = x . first ; int cost = x . second ; if ( dist [ v ] > dist [ u ] + cost ) { dist [ v ] = dist [ u ] + cost ; if ( ! inQueue [ v ] ) { q . Enqueue ( v ) ; inQueue [ v ] = true ; cnt [ v ] ++ ; if ( cnt [ v ] >= V ) return true ; } } } } return false ; } public static void Main ( String [ ] args ) { int N = 4 ; int src = 0 ; int M = 4 ; int [ , ] Edges = { { 0 , 1 , 1 } , { 1 , 2 , - 1 } , { 2 , 3 , - 1 } , { 3 , 0 , - 1 } } ; if ( sfpa ( N , src , Edges , M ) == true ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } }
Sum of all pair shortest paths in a Tree | C # program for the above approach ; Function that performs the Floyd Warshall to find all shortest paths ; Initialize the distance matrix ; Pick all vertices as source one by one ; Pick all vertices as destination for the above picked source ; If vertex k is on the shortest path from i to j then update dist [ i , j ] ; Sum the upper diagonal of the shortest distance matrix ; Traverse the given dist [ , ] ; Add the distance ; Return the readonly sum ; Function to generate the tree ; Add edges ; Get source and destination with weight ; Add the edges ; Perform Floyd Warshal Algorithm ; Driver code ; Number of Vertices ; Number of Edges ; Given Edges with weight ; Function Call
using System ; class GFG { static readonly int INF = 99999 ; static int floyd_warshall ( int [ , ] graph , int V ) { int [ , ] dist = new int [ V , V ] ; int i , j , k ; for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { dist [ i , j ] = graph [ i , j ] ; } } for ( k = 0 ; k < V ; k ++ ) { for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { if ( dist [ i , k ] + dist [ k , j ] < dist [ i , j ] ) { dist [ i , j ] = dist [ i , k ] + dist [ k , j ] ; } } } } int sum = 0 ; for ( i = 0 ; i < V ; i ++ ) { for ( j = i + 1 ; j < V ; j ++ ) { sum += dist [ i , j ] ; } } return sum ; } static int sumOfshortestPath ( int N , int E , int [ , ] edges ) { int [ , ] g = new int [ N , N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { g [ i , j ] = INF ; } } for ( int i = 0 ; i < E ; i ++ ) { int u = edges [ i , 0 ] ; int v = edges [ i , 1 ] ; int w = edges [ i , 2 ] ; g [ u , v ] = w ; g [ v , u ] = w ; } return floyd_warshall ( g , N ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int E = 3 ; int [ , ] Edges = { { 0 , 1 , 1 } , { 1 , 2 , 2 } , { 2 , 3 , 3 } } ; Console . Write ( sumOfshortestPath ( N , E , Edges ) ) ; } }
Kth space | C # program for the above approach ; Function to print kth integer in a given string ; Size of the string ; Pointer for iteration ; If space char found decrement K ; If K becomes 1 , the next string is the required one ; Print the required number ; Driver Code ; Given string ; Given K ; Function call
using System ; class GFG { static void print_kth_string ( string s , int K ) { int N = s . Length ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ▁ ' ) K -- ; if ( K == 1 ) break ; } while ( i ++ < N - 1 && s [ i ] != ' ▁ ' ) Console . Write ( s [ i ] ) ; } public static void Main ( ) { string s = "10 ▁ 20 ▁ 30 ▁ 40" ; int K = 4 ; print_kth_string ( s , K ) ; } }
Minimum peak elements from an array by their repeated removal at every iteration of the array | C # program for the above approach ; Function to return the list of minimum peak elements ; Length of original list ; Initialize resultant list ; Traverse each element of list ; Length of original list after removing the peak element ; Traverse new list after removal of previous min peak element ; Update min and index , if first element of list > next element ; Update min and index , if last element of list > previous one ; Update min and index , if list has single element ; Update min and index , if current element > adjacent elements ; Remove current min peak element from list ; Insert min peak into resultant list ; Print resultant list ; Driver Code ; Given array [ ] arr ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void minPeaks ( List < int > list ) { int n = list . Count ; List < int > result = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int min = int . MaxValue ; int index = - 1 ; int size = list . Count ; for ( int j = 0 ; j < size ; j ++ ) { if ( j == 0 && j + 1 < size ) { if ( list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( j == size - 1 && j - 1 >= 0 ) { if ( list [ j ] > list [ j - 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( size == 1 ) { min = list [ j ] ; index = j ; } else if ( list [ j ] > list [ j - 1 ] && list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } list . RemoveAt ( index ) ; result . Add ( min ) ; } for ( int i = 0 ; i < result . Count ; i ++ ) Console . Write ( result [ i ] + " , ▁ " ) ; } public static void Main ( String [ ] args ) { List < int > arr = new List < int > { 1 , 9 , 7 , 8 , 2 , 6 } ; minPeaks ( arr ) ; } }
Search an element in a reverse sorted array | C # Program to implement the above approach ; Function to search if element X is present in reverse sorted array ; Store the first index of the subarray in which X lies ; Store the last index of the subarray in which X lies ; Store the middle index of the subarray ; Check if value at middle index of the subarray equal to X ; Element is found ; If X is smaller than the value at middle index of the subarray ; Search in right half of subarray ; Search in left half of subarray ; If X not found ; Driver code
using System ; class GFG { static int binarySearch ( int [ ] arr , int N , int X ) { int start = 0 ; int end = N ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( X == arr [ mid ] ) { return mid ; } else if ( X < arr [ mid ] ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . Length ; int X = 5 ; Console . WriteLine ( binarySearch ( arr , N , X ) ) ; } }
Construct an AP series consisting of A and B having minimum possible Nth term | C # program for the above approach ; Function to check if both a and b are present in the AP series or not ; Iterate over the array arr [ ] ; If a is present ; If b is present ; If both are present ; Otherwise ; Function to print all the elements of the Arithmetic Progression ; Function to construct AP series consisting of A and B with minimum Nth term ; Stores the resultant series ; Initialise ans [ i ] as INT_MAX ; Maintain a smaller than b ; Swap a and b ; Difference between a and b ; Check for all possible combination of start and common difference d ; Initialise arr [ 0 ] as start ; Check if both a and b are present or not and the Nth term is the minimum or not ; Update the answer ; Print the resultant array ; Driver Code ; Function call
using System ; class GFG { static bool check_both_present ( int [ ] arr , int N , int a , int b ) { bool f1 = false , f2 = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == a ) { f1 = true ; } if ( arr [ i ] == b ) { f2 = true ; } } if ( f1 && f2 ) { return true ; } else { return false ; } } static void print_array ( int [ ] ans , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } static void build_AP ( int N , int a , int b ) { int [ ] arr = new int [ N ] ; int [ ] ans = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) ans [ i ] = int . MaxValue ; if ( a > b ) { a += ( b - ( b = a ) ) ; } int diff = b - a ; for ( int start = 1 ; start <= a ; start ++ ) { for ( int d = 1 ; d <= diff ; d ++ ) { arr [ 0 ] = start ; for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + d ; } if ( check_both_present ( arr , N , a , b ) && arr [ N - 1 ] < ans [ N - 1 ] ) { for ( int i = 0 ; i < N ; i ++ ) { ans [ i ] = arr [ i ] ; } } } } print_array ( ans , N ) ; } static public void Main ( ) { int N = 5 , A = 20 , B = 50 ; build_AP ( N , A , B ) ; } }
Find the word from a given sentence having given word as prefix | C # program for the above approach ; Function to find the position of the string having word as prefix ; Initialize an List ; Extract words from the sentence ; Traverse each word ; Traverse the characters of word ; If prefix does not match ; Otherwise ; Return the word ; Return - 1 if not present ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String isPrefixOfWord ( String sentence , String Word ) { String [ ] a = sentence . Split ( ' ▁ ' ) ; List < String > v = new List < String > ( ) ; foreach ( String e in a ) v . Add ( e ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { for ( int j = 0 ; j < v [ i ] . Length ; j ++ ) { if ( v [ i ] [ j ] != Word [ j ] ) break ; else if ( j == Word . Length - 1 ) return v [ i ] ; } } return " - 1" ; } public static void Main ( String [ ] args ) { String s = " Welcome ▁ to ▁ Geeksforgeeks " ; String word = " Gee " ; Console . WriteLine ( isPrefixOfWord ( s , word ) ) ; } }
Paths requiring minimum number of jumps to reach end of array | C # program to implement the above approach ; Pair Struct ; Stores the current index ; Stores the path travelled so far ; Stores the minimum jumps required to reach the last index from current index ; Constructor ; Minimum jumps required to reach end of the array ; Stores the maximum number of steps that can be taken from the current index ; Checking if index stays within bounds ; Stores the minimum number of jumps required to jump from ( i + j ) - th index ; Function to find all possible paths to reach end of array requiring minimum number of steps ; Storing the neighbours of current index element ; Function to find the minimum steps and corresponding paths to reach end of an array ; Stores the minimum jumps from each position to last position ; Driver Code
using System ; using System . Collections ; public struct Pair { public int idx ; public string psf ; public int jmps ; public Pair ( int idx , String psf , int jmps ) { this . idx = idx ; this . psf = psf ; this . jmps = jmps ; } } class GFG { public static int [ ] minJumps ( int [ ] arr ) { int [ ] dp = new int [ arr . Length ] ; int n = dp . Length ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = int . MaxValue ; dp [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int steps = arr [ i ] ; int min = int . MaxValue ; for ( int j = 1 ; j <= steps && i + j < n ; j ++ ) { if ( dp [ i + j ] != int . MaxValue && dp [ i + j ] < min ) { min = dp [ i + j ] ; } } if ( min != int . MaxValue ) dp [ i ] = min + 1 ; } return dp ; } public static void possiblePath ( int [ ] arr , int [ ] dp ) { Queue queue = new Queue ( ) ; queue . Enqueue ( new Pair ( 0 , "0" , dp [ 0 ] ) ) ; while ( queue . Count > 0 ) { Pair tmp = ( Pair ) queue . Dequeue ( ) ; if ( tmp . jmps == 0 ) { Console . WriteLine ( tmp . psf ) ; continue ; } for ( int step = 1 ; step <= arr [ tmp . idx ] ; step ++ ) { if ( tmp . idx + step < arr . Length && tmp . jmps - 1 == dp [ tmp . idx + step ] ) { queue . Enqueue ( new Pair ( tmp . idx + step , tmp . psf + " ▁ - > ▁ " + ( tmp . idx + step ) , tmp . jmps - 1 ) ) ; } } } } public static void Solution ( int [ ] arr ) { int [ ] dp = minJumps ( arr ) ; possiblePath ( arr , dp ) ; } static public void Main ( ) { int [ ] arr = { 3 , 3 , 0 , 2 , 1 , 2 , 4 , 2 , 0 , 0 } ; int size = arr . Length ; Solution ( arr ) ; } }
Length of longest increasing absolute even subsequence | C # program for the above approach ; Function to find the longest increasing absolute even subsequence ; Length of arr ; Stores length of required subsequence ; Traverse the array ; Traverse prefix of current array element ; Check if the subsequence is LIS and have even absolute difference of adjacent pairs ; Update lis [ ] ; Stores maximum length ; Find the length of longest absolute even subsequence ; Return the maximum length of absolute even subsequence ; Driver code ; Given array [ ] arr and brr [ ] ; Function call
using System ; class GFG { static void EvenLIS ( int [ ] arr ) { int n = arr . Length ; int [ ] lis = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( Math . Abs ( arr [ i ] ) > Math . Abs ( arr [ j ] ) && Math . Abs ( arr [ i ] ) % 2 == 0 && Math . Abs ( arr [ j ] ) % 2 == 0 && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } int maxlen = 0 ; for ( int i = 0 ; i < n ; i ++ ) maxlen = Math . Max ( maxlen , lis [ i ] ) ; Console . WriteLine ( maxlen ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 11 , - 22 , 43 , - 54 , 66 , 5 } ; int N = arr . Length ; EvenLIS ( arr ) ; } }
Count balanced nodes present in a binary tree | C # program to implement the above approach ; Structure of a Tree Node ; Function to get the sum of left subtree and right subtree ; Base case ; Store the sum of left subtree ; Store the sum of right subtree ; Check if node is balanced or not ; Increase count of balanced nodes ; Return subtree sum ; Driver Code ; Insert nodes in tree ; Store the count of balanced nodes
using System ; class GFG { static int res = 0 ; public class Node { public int data ; public Node left ; public Node right ; public Node ( int val ) { data = val ; left = right = null ; } } ; static int Sum ( Node root ) { if ( root == null ) { return 0 ; } int leftSubSum = Sum ( root . left ) ; int rightSubSum = Sum ( root . right ) ; if ( root . left != null && root . right != null && leftSubSum == rightSubSum ) res += 1 ; return root . data + leftSubSum + rightSubSum ; } public static void Main ( String [ ] args ) { Node root = new Node ( 9 ) ; root . left = new Node ( 2 ) ; root . left . left = new Node ( - 1 ) ; root . left . right = new Node ( 3 ) ; root . right = new Node ( 4 ) ; root . right . right = new Node ( 0 ) ; res = 0 ; Sum ( root ) ; Console . Write ( res ) ; } }
Print alternate nodes from all levels of a Binary Tree | C # program to implement the above approach ; Structure of a Node ; Print alternate nodes of a binary tree ; Store nodes of each level ; Store count of nodes of current level ; Print alternate nodes of the current level ; If left child exists ; Store left child ; If right child exists ; Store right child ; Driver Code ; Create a tree
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left ; public Node right ; public Node ( int val ) { data = val ; left = right = null ; } } ; static void PrintAlternate ( Node root ) { Queue < Node > Q = new Queue < Node > ( ) ; Q . Enqueue ( root ) ; while ( Q . Count != 0 ) { int N = Q . Count ; for ( int i = 0 ; i < N ; i ++ ) { Node temp = Q . Peek ( ) ; Q . Dequeue ( ) ; if ( i % 2 == 0 ) { Console . Write ( temp . data + " ▁ " ) ; } if ( temp . left != null ) { Q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { Q . Enqueue ( temp . right ) ; } } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { Node root ; root = new Node ( 71 ) ; root . left = new Node ( 88 ) ; root . right = new Node ( 99 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 11 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; PrintAlternate ( root ) ; } }