codeparrot/xlcost-text-to-code huggingface dataset:
Difference between sums of odd level and even level nodes of a Binary Tree | C # program to find difference between sums of odd level and even level nodes of binary tree ; User defined node class ; Constructor to create a new tree node ; return difference of sums of odd level and even level ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if level no . is even or odd and accordingly update the evenSum or oddSum ; check for left child ; check for right child ; Driver code ; construct a tree
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int key ) { data = key ; left = right = null ; } } public class GFG { static int evenOddLevelDifference ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; int level = 0 ; int evenSum = 0 , oddSum = 0 ; while ( q . Count != 0 ) { int size = q . Count ; level ++ ; while ( size > 0 ) { Node temp = q . Dequeue ( ) ; if ( level % 2 == 0 ) evenSum += temp . data ; else oddSum += temp . data ; if ( temp . left != null ) q . Enqueue ( temp . left ) ; if ( temp . right != null ) q . Enqueue ( temp . right ) ; size -- ; } } return ( oddSum - evenSum ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 5 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 6 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 4 ) ; root . left . right . left = new Node ( 3 ) ; root . right . right = new Node ( 8 ) ; root . right . right . right = new Node ( 9 ) ; root . right . right . left = new Node ( 7 ) ; Console . WriteLine ( " diffence ▁ between ▁ sums ▁ is ▁ " + evenOddLevelDifference ( root ) ) ; } }
Difference between sums of odd level and even level nodes of a Binary Tree | A recursive C # program to find difference between sum of nodes at odd level and sum at even level ; A binary tree node ; The main function that return difference between odd and even level nodes ; Base case ; Difference for root is root 's   data - difference for left subtree  - difference for right subtree  ; Driver program to test above functions
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right ; } } public class BinaryTree { public Node root ; public virtual int getLevelDiff ( Node node ) { if ( node == null ) { return 0 ; } return node . data - getLevelDiff ( node . left ) - getLevelDiff ( node . right ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . left . right . left = new Node ( 3 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 7 ) ; Console . WriteLine ( tree . getLevelDiff ( tree . root ) + " ▁ is ▁ the ▁ required ▁ difference " ) ; } }
Maximum spiral sum in Binary Tree | C # implementation to find maximum spiral sum ; A binary tree node has data , pointer to left child and a pointer to right child ; function to find the maximum sum contiguous subarray . implements kadane 's algorithm  ; to store the maximum value that is ending up to the current index ; to store the maximum value encountered so far ; traverse the array elements ; if max_ending_here < 0 , then it could not possibly contribute to the maximum sum further ; else add the value arr [ i ] to max_ending_here ; update max_so_far ; required maxium sum contiguous subarray value ; Function to find maximum spiral sum ; if tree is empty ; Create two stacks to store alternate levels For levels from right to left ; For levels from left to right ; ArrayList to store spiral order traversal of the binary tree ; Push first level to first stack ' s1' ; traversing tree in spiral form until there are elements in any one of the stacks ; traverse current level from s1 and push nodes of next level to s2 ; push temp - data to ' arr ' ; Note that right is pushed before left ; traverse current level from s2 and push nodes of next level to s1 ; push temp - data to ' arr ' ; Note that left is pushed before right ; required maximum spiral sum ; Driver code
using System ; using System . Collections . Generic ; public class MaxSpiralSum { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } ; static int maxSum ( List < int > arr ) { int max_ending_here = int . MinValue ; int max_so_far = int . MinValue ; for ( int i = 0 ; i < arr . Count ; i ++ ) { if ( max_ending_here < 0 ) max_ending_here = arr [ i ] ; else max_ending_here += arr [ i ] ; max_so_far = Math . Max ( max_so_far , max_ending_here ) ; } return max_so_far ; } public static int maxSpiralSum ( Node root ) { if ( root == null ) return 0 ; Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; List < int > arr = new List < int > ( ) ; s1 . Push ( root ) ; while ( s1 . Count != 0 s2 . Count != 0 ) { while ( s1 . Count != 0 ) { Node temp = s1 . Pop ( ) ; arr . Add ( temp . data ) ; if ( temp . right != null ) s2 . Push ( temp . right ) ; if ( temp . left != null ) s2 . Push ( temp . left ) ; } while ( s2 . Count != 0 ) { Node temp = s2 . Pop ( ) ; arr . Add ( temp . data ) ; if ( temp . left != null ) s1 . Push ( temp . left ) ; if ( temp . right != null ) s1 . Push ( temp . right ) ; } } return maxSum ( arr ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( - 2 ) ; root . left = new Node ( - 3 ) ; root . right = new Node ( 4 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( - 2 ) ; root . right . right = new Node ( - 1 ) ; root . left . left . left = new Node ( - 3 ) ; root . right . right . right = new Node ( 2 ) ; Console . Write ( " Maximum ▁ Spiral ▁ Sum ▁ = ▁ " + maxSpiralSum ( root ) ) ; } }
Sum of all leaf nodes of binary tree | C # program to find sum of all leaf nodes of binary tree ; user define class node ; constructor ; utility function which calculates sum of all leaf nodes ; add root data to sum if root is a leaf node ; propagate recursively in left and right subtree ; driver program ; construct binary tree ; variable to store sum of leaf nodes
using System ; public class GFG { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } public static int sum ; public static void leafSum ( Node root ) { if ( root == null ) { return ; } if ( root . left == null && root . right == null ) { sum += root . data ; } leafSum ( root . left ) ; leafSum ( root . right ) ; } public static void Main ( string [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right = new Node ( 3 ) ; root . right . right = new Node ( 7 ) ; root . right . left = new Node ( 6 ) ; root . right . left . right = new Node ( 8 ) ; sum = 0 ; leafSum ( root ) ; Console . WriteLine ( sum ) ; } }
Inorder Tree Traversal without recursion and without stack ! | C # program to print inorder traversal without recursion and stack ; A binary tree tNode has data , pointer to left child and a pointer to right child ; Function to traverse binary tree without recursion and without stack ; Find the inorder predecessor of current ; Make current as right child of its inorder predecessor ; Revert the changes made in if part to restore the original tree i . e . , fix the right child of predecssor ; Driver code ; Constructed binary tree is 1 / \ 2 3 / \ 4 5
using System ; class BinaryTree { tNode root ; public class tNode { public int data ; public tNode left , right ; public tNode ( int item ) { data = item ; left = right = null ; } } void MorrisTraversal ( tNode root ) { tNode current , pre ; if ( root == null ) return ; current = root ; while ( current != null ) { if ( current . left == null ) { Console . Write ( current . data + " ▁ " ) ; current = current . right ; } else { pre = current . left ; while ( pre . right != null && pre . right != current ) pre = pre . right ; if ( pre . right == null ) { pre . right = current ; current = current . left ; } else { pre . right = null ; Console . Write ( current . data + " ▁ " ) ; current = current . right ; } } } } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new tNode ( 1 ) ; tree . root . left = new tNode ( 2 ) ; tree . root . right = new tNode ( 3 ) ; tree . root . left . left = new tNode ( 4 ) ; tree . root . left . right = new tNode ( 5 ) ; tree . MorrisTraversal ( tree . root ) ; } }
Sum of leaf nodes at minimum level | C # implementation to find the sum of leaf nodes at minimum level ; structure of a node of binary tree ; function to get a new node ; allocate space ; put in the data ; function to find the sum of leaf nodes at minimum level ; if tree is empty ; if there is only one node ; queue used for level order traversal ; push root node in the queue ' q ' ; count number of nodes in the current level ; traverse the current level nodes ; get front element from ' q ' ; if it is a leaf node ; accumulate data to ' sum ' ; set flag ' f ' to 1 , to signify minimum level for leaf nodes has been encountered ; if top ' s ▁ left ▁ and ▁ right ▁ child ▁ ▁ ▁ exists , ▁ then ▁ push ▁ them ▁ to ▁ ' q ' ; required sum ; Driver Code ; binary tree creation
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static int sumOfLeafNodesAtMinLevel ( Node root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return root . data ; Queue < Node > q = new Queue < Node > ( ) ; int sum = 0 ; bool f = false ; q . Enqueue ( root ) ; while ( f == false ) { int nc = q . Count ; while ( nc -- > 0 ) { Node top = q . Peek ( ) ; q . Dequeue ( ) ; if ( top . left == null && top . right == null ) { sum += top . data ; f = true ; } else { if ( top . left != null ) q . Enqueue ( top . left ) ; if ( top . right != null ) q . Enqueue ( top . right ) ; } } } return sum ; } public static void Main ( String [ ] args ) { Node root = getNode ( 1 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 4 ) ; root . left . right = getNode ( 5 ) ; root . right . left = getNode ( 6 ) ; root . right . right = getNode ( 7 ) ; root . left . right . left = getNode ( 8 ) ; root . right . left . right = getNode ( 9 ) ; Console . WriteLine ( " Sum ▁ = ▁ " + sumOfLeafNodesAtMinLevel ( root ) ) ; } }
Root to leaf path sum equal to a given number | C # program to print to print root to leaf path sum equal to a given number ; A binary tree node has data , pointer to left child and a pointer to right child ; Given a tree and a sum , return true if there is a path from the root down to a leaf , such that adding up all the values along the path equals the given sum . Strategy : subtract the node value from the sum when recurring down , and check to see if the sum is 0 when you run out of tree . ; Driver Code ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual bool haspathSum ( Node node , int sum ) { if ( node == null ) { return ( sum == 0 ) ; } else { bool ans = false ; int subsum = sum - node . data ; if ( subsum == 0 && node . left == null && node . right == null ) { return true ; } if ( node . left != null ) { ans = ans || haspathSum ( node . left , subsum ) ; } if ( node . right != null ) { ans = ans || haspathSum ( node . right , subsum ) ; } return ans ; } } public static void Main ( string [ ] args ) { int sum = 21 ; GFG tree = new GFG ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 2 ) ; if ( tree . haspathSum ( tree . root , sum ) ) { Console . WriteLine ( " There ▁ is ▁ a ▁ root ▁ to ▁ leaf ▁ " + " path ▁ with ▁ sum ▁ " + sum ) ; } else { Console . WriteLine ( " There ▁ is ▁ no ▁ root ▁ to ▁ leaf ▁ " + " path ▁ with ▁ sum ▁ " + sum ) ; } } }
Sum of all the numbers that are formed from root to leaf paths | c # program to find sum of all numbers that are formed from root to leaf paths ; Returns sum of all root to leaf paths . The first parameter is root of current subtree , the second parameter is value of the number formed by nodes from root to this node ; Base case ; Update val ; if current node is leaf , return the current value of val ; recur sum of values for left and right subtree ; A wrapper function over treePathsSumUtil ( ) ; Pass the initial value as 0 as there is nothing above root ; Driver Code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual int treePathsSumUtil ( Node node , int val ) { if ( node == null ) { return 0 ; } val = ( val * 10 + node . data ) ; if ( node . left == null && node . right == null ) { return val ; } return treePathsSumUtil ( node . left , val ) + treePathsSumUtil ( node . right , val ) ; } public virtual int treePathsSum ( Node node ) { return treePathsSumUtil ( node , 0 ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 6 ) ; tree . root . left = new Node ( 3 ) ; tree . root . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 4 ) ; tree . root . left . left = new Node ( 2 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . left . right . right = new Node ( 4 ) ; tree . root . left . right . left = new Node ( 7 ) ; Console . Write ( " Sum ▁ of ▁ all ▁ paths ▁ is ▁ " + tree . treePathsSum ( tree . root ) ) ; } }
Merge Two Binary Trees by doing Node Sum ( Recursive and Iterative ) | C # program to Merge Two Binary Trees ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper method that allocates a new node with the given data and NULL left and right pointers . ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Method to merge given two binary trees ; Driver Code ; Let us construct the first Binary Tree 1 / \ 2 3 / \ \ 4 5 6 ; Let us construct the second Binary Tree 4 / \ 1 7 / / \ 3 2 6
using System ; public class Node { public int data ; public Node left , right ; public Node ( int data , Node left , Node right ) { this . data = data ; this . left = left ; this . right = right ; } public static Node newNode ( int data ) { return new Node ( data , null , null ) ; } public static void inorder ( Node node ) { if ( node == null ) { return ; } inorder ( node . left ) ; Console . Write ( " { 0 : D } ▁ " , node . data ) ; inorder ( node . right ) ; } public static Node MergeTrees ( Node t1 , Node t2 ) { if ( t1 == null ) { return t2 ; } if ( t2 == null ) { return t1 ; } t1 . data += t2 . data ; t1 . left = MergeTrees ( t1 . left , t2 . left ) ; t1 . right = MergeTrees ( t1 . right , t2 . right ) ; return t1 ; } public static void Main ( string [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; root1 . right . right = newNode ( 6 ) ; Node root2 = newNode ( 4 ) ; root2 . left = newNode ( 1 ) ; root2 . right = newNode ( 7 ) ; root2 . left . left = newNode ( 3 ) ; root2 . right . left = newNode ( 2 ) ; root2 . right . right = newNode ( 6 ) ; Node root3 = MergeTrees ( root1 , root2 ) ; Console . Write ( " The ▁ Merged ▁ Binary ▁ Tree ▁ is : STRNEWLINE " ) ; inorder ( root3 ) ; } }
Merge Two Binary Trees by doing Node Sum ( Recursive and Iterative ) | C # program to Merge Two Binary Trees ; A binary tree node has data , pointer to left child and a pointer to right child ; Structure to store node pair onto stack ; Helper function that allocates a new node with the given data and null left and right pointers . ; Given a binary tree , print its nodes in inorder ; First recur on left child ; Then print the data of node ; Now recur on right child ; Function to merge given two binary trees ; Driver code ; Let us conthe first Binary Tree 1 / \ 2 3 / \ \ 4 5 6 ; Let us conthe second Binary Tree 4 / \ 1 7 / / \ 3 2 6
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } ; public class snode { public Node l , r ; } ; static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . left = new_node . right = null ; return new_node ; } static void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inorder ( node . right ) ; } static Node MergeTrees ( Node t1 , Node t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; Stack < snode > s = new Stack < snode > ( ) ; snode temp = new snode ( ) ; temp . l = t1 ; temp . r = t2 ; s . Push ( temp ) ; snode n ; while ( s . Count != 0 ) { n = s . Peek ( ) ; s . Pop ( ) ; if ( n . l == null n . r == null ) continue ; n . l . data += n . r . data ; if ( n . l . left == null ) n . l . left = n . r . left ; else { snode t = new snode ( ) ; t . l = n . l . left ; t . r = n . r . left ; s . Push ( t ) ; } if ( n . l . right == null ) n . l . right = n . r . right ; else { snode t = new snode ( ) ; t . l = n . l . right ; t . r = n . r . right ; s . Push ( t ) ; } } return t1 ; } public static void Main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; root1 . right . right = newNode ( 6 ) ; Node root2 = newNode ( 4 ) ; root2 . left = newNode ( 1 ) ; root2 . right = newNode ( 7 ) ; root2 . left . left = newNode ( 3 ) ; root2 . right . left = newNode ( 2 ) ; root2 . right . right = newNode ( 6 ) ; Node root3 = MergeTrees ( root1 , root2 ) ; Console . Write ( " The ▁ Merged ▁ Binary ▁ Tree ▁ is : STRNEWLINE " ) ; inorder ( root3 ) ; } }
Find root of the tree where children id sum for every node is given | C # Find root of tree where children sum for every node id is given . ; Every node appears once as an id , and every node except for the root appears once in a sum . So if we subtract all the sums from all the ids , we 're left  with the root id. ; Driver code
using System ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findRoot ( pair [ ] arr , int n ) { int root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { root += ( arr [ i ] . first - arr [ i ] . second ) ; } return root ; } public static void Main ( String [ ] args ) { pair [ ] arr = { new pair ( 1 , 5 ) , new pair ( 2 , 0 ) , new pair ( 3 , 0 ) , new pair ( 4 , 0 ) , new pair ( 5 , 5 ) , new pair ( 6 , 5 ) } ; int n = arr . Length ; Console . Write ( " { 0 } STRNEWLINE " , findRoot ( arr , n ) ) ; } }
Print Postorder traversal from given Inorder and Preorder traversals | C # program to print postorder traversal from preorder and inorder traversals ; A utility function to search x in [ ] arr of size n ; Prints postorder traversal from given inorder and preorder traversals ; The first element in pre [ ] is always root , search it in in [ ] to find left and right subtrees ; If left subtree is not empty , print left subtree ; If right subtree is not empty , print right subtree ; Print root ; Driver code
using System ; class GFG { static int search ( int [ ] arr , int x , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return - 1 ; } static void printPostOrder ( int [ ] in1 , int [ ] pre , int n ) { int root = search ( in1 , pre [ 0 ] , n ) ; int [ ] ar ; if ( root != 0 ) { ar = new int [ n - 1 ] ; Array . Copy ( pre , 1 , ar , 0 , n - 1 ) ; printPostOrder ( in1 , ar , root ) ; } if ( root != n - 1 ) { ar = new int [ n - ( root + 1 ) ] ; Array . Copy ( in1 , root + 1 , ar , 0 , n - ( root + 1 ) ) ; int [ ] ar1 = new int [ n - ( root + 1 ) ] ; Array . Copy ( pre , root + 1 , ar1 , 0 , n - ( root + 1 ) ) ; printPostOrder ( ar , ar1 , n - root - 1 ) ; } Console . Write ( pre [ 0 ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] in1 = { 4 , 2 , 5 , 1 , 3 , 6 } ; int [ ] pre = { 1 , 2 , 4 , 5 , 3 , 6 } ; int n = in1 . Length ; Console . WriteLine ( " Postorder ▁ traversal ▁ " ) ; printPostOrder ( in1 , pre , n ) ; } }
Lowest Common Ancestor in a Binary Tree | Set 1 | C # implementation to find lowest common ancestor of n1 and n2 using one traversal of binary tree ; Class containing left and right child of current node and key value ; Root of the Binary Tree ; This function returns pointer to LCA of two given values n1 and n2 . This function assumes that n1 and n2 are present in Binary Tree ; Base case ; If either n1 or n2 matches with root 's key, report the presence  by returning root (Note that if  a key is ancestor of other,  then the ancestor key becomes LCA ; Look for keys in left and right subtrees ; If both of the above calls return Non - NULL , then one key is present in once subtree and other is present in other , So this node is the LCA ; Otherwise check if left subtree or right subtree is LCA ; Driver code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; Node findLCA ( int n1 , int n2 ) { return findLCA ( root , n1 , n2 ) ; } Node findLCA ( Node node , int n1 , int n2 ) { if ( node == null ) return null ; if ( node . data == n1 node . data == n2 ) return node ; Node left_lca = findLCA ( node . left , n1 , n2 ) ; Node right_lca = findLCA ( node . right , n1 , n2 ) ; if ( left_lca != null && right_lca != null ) return node ; return ( left_lca != null ) ? left_lca : right_lca ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; Console . WriteLine ( " LCA ( 4 , ▁ 5 ) ▁ = ▁ " + tree . findLCA ( 4 , 5 ) . data ) ; Console . WriteLine ( " LCA ( 4 , ▁ 6 ) ▁ = ▁ " + tree . findLCA ( 4 , 6 ) . data ) ; Console . WriteLine ( " LCA ( 3 , ▁ 4 ) ▁ = ▁ " + tree . findLCA ( 3 , 4 ) . data ) ; Console . WriteLine ( " LCA ( 2 , ▁ 4 ) ▁ = ▁ " + tree . findLCA ( 2 , 4 ) . data ) ; } }