codeparrot/xlcost-text-to-code huggingface dataset:
Print nodes between two given level numbers of a binary tree | Java program to print Nodes level by level between given two levels ; A binary tree Node has key , pointer to left and right children ; Given a binary tree , print nodes from level number ' low ' to level number ' high ' ; Marker node to indicate end of level ; Initialize level number ; Enqueue the only first level node and marker node for end of level ; Simple level order traversal loop ; Remove the front item from queue ; Check if end of level is reached ; print a new line and increment level number ; Check if marker node was last node in queue or level number is beyond the given upper limit ; Enqueue the marker for end of next level ; If this is marker , then we don 't need print it  and enqueue its children ; If level is equal to or greater than given lower level , print it ; Enqueue children of non - marker node ; Driver program to test for above functions
import java . util . LinkedList ; import java . util . Queue ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printLevels ( Node node , int low , int high ) { Queue < Node > Q = new LinkedList < > ( ) ; Node marker = new Node ( 4 ) ; int level = 1 ; Q . add ( node ) ; Q . add ( marker ) ; while ( Q . isEmpty ( ) == false ) { Node n = Q . peek ( ) ; Q . remove ( ) ; if ( n == marker ) { System . out . println ( " " ) ; level ++ ; if ( Q . isEmpty ( ) == true level > high ) break ; Q . add ( marker ) ; continue ; } if ( level >= low ) System . out . print ( n . data + " ▁ " ) ; if ( n . left != null ) Q . add ( n . left ) ; if ( n . right != null ) Q . add ( n . right ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 22 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; System . out . print ( " Level ▁ Order ▁ traversal ▁ between ▁ given ▁ two ▁ levels ▁ is ▁ " ) ; tree . printLevels ( tree . root , 2 , 3 ) ; } }
Print nodes at k distance from root | A binary tree node has data , pointer to left child and a pointer to right child ; Driver program to test above functions ; Constructed binary tree is 1 / \ 2 3 / \ / 4 5 8
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printKDistant ( Node node , int k ) { if ( node == null k < 0 ) return ; if ( k == 0 ) { System . out . print ( node . data + " ▁ " ) ; return ; } printKDistant ( node . left , k - 1 ) ; printKDistant ( node . right , k - 1 ) ; } 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 ( 8 ) ; tree . printKDistant ( tree . root , 2 ) ; } }
Print nodes at k distance from root | Iterative | Java program to print all nodes of level k iterative approach ; Node of binary tree ; Function to add a new node ; Function to print nodes of given level ; extra null is added to keep track of all the nodes to be added before level is incremented by 1 ; print when level is equal to k ; break the loop if level exceeds the given level number ; Driver code ; create a binary tree
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node newnode = new Node ( ) ; newnode . data = data ; newnode . left = newnode . right = null ; return newnode ; } static boolean printKDistant ( Node root , int klevel ) { Queue < Node > q = new LinkedList < > ( ) ; int level = 1 ; boolean flag = false ; q . add ( root ) ; q . add ( null ) ; while ( q . size ( ) > 0 ) { Node temp = q . peek ( ) ; if ( level == klevel && temp != null ) { flag = true ; System . out . print ( temp . data + " ▁ " ) ; } q . remove ( ) ; if ( temp == null ) { if ( q . peek ( ) != null ) q . add ( null ) ; level += 1 ; if ( level > klevel ) break ; } else { if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } } System . out . println ( ) ; return flag ; } public static void main ( String srga [ ] ) { Node root = newNode ( 20 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 15 ) ; root . left . right . left = newNode ( 12 ) ; root . right . left = newNode ( 25 ) ; root . right . right = newNode ( 40 ) ; System . out . print ( " data ▁ at ▁ level ▁ 1 ▁ : ▁ " ) ; boolean ret = printKDistant ( root , 1 ) ; if ( ret == false ) System . out . print ( " Number ▁ exceeds ▁ total ▁ " + "number of levelsNEW_LINE"); System . out . print ( " data ▁ at ▁ level ▁ 2 ▁ : ▁ " ) ; ret = printKDistant ( root , 2 ) ; if ( ret == false ) System . out . print ( " Number ▁ exceeds ▁ total ▁ " + "number of levelsNEW_LINE"); System . out . print ( " data ▁ at ▁ level ▁ 3 ▁ : ▁ " ) ; ret = printKDistant ( root , 3 ) ; if ( ret == false ) System . out . print ( " Number ▁ exceeds ▁ total ▁ " + "number of levelsNEW_LINE"); System . out . print ( " data ▁ at ▁ level ▁ 6 ▁ : ▁ " ) ; ret = printKDistant ( root , 6 ) ; if ( ret == false ) System . out . print ( " Number ▁ exceeds ▁ total " + "number of levelsNEW_LINE"); } }
Print all leaf nodes of a Binary Tree from left to right | Java program to print leaf nodes from left to right ; A Binary Tree Node ; Function to print leaf nodes from left to right ; If node is null , return ; If node is leaf node , print its data ; If left child exists , check for leaf recursively ; If right child exists , check for leaf recursively ; Utility function to create a new tree node ; Driver code ; Let us create binary tree shown in above diagram ; Print leaf nodes of the given tree
import java . util . * ; class GFG { static class Node { public int data ; public Node left , right ; } ; static void printLeafNodes ( Node root ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { System . out . print ( root . data + " ▁ " ) ; return ; } if ( root . left != null ) printLeafNodes ( root . left ) ; if ( root . right != null ) printLeafNodes ( root . right ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 8 ) ; root . right . left . left = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . left = newNode ( 9 ) ; root . right . right . right = newNode ( 10 ) ; printLeafNodes ( root ) ; } }
Print all nodes at distance k from a given node | A binary tree node ; Recursive function to print all the nodes at distance k in tree ( or subtree ) rooted with given root . ; Base Case ; If we reach a k distant node , print it ; Recur for left and right subtrees ; Prints all nodes at distance k from a given target node . The k distant nodes may be upward or downward . This function Returns distance of root from target node , it returns - 1 if target node is not present in tree rooted with root . ; Base Case 1 : If tree is empty , return - 1 ; If target is same as root . Use the downward function to print all nodes at distance k in subtree rooted with target or root ; Recur for left subtree ; Check if target node was found in left subtree ; If root is at distance k from target , print root Note that dl is Distance of root 's left child from  target ; Else go to right subtree and print all k - dl - 2 distant nodes Note that the right child is 2 edges away from left child ; Add 1 to the distance and return value for parent calls ; MIRROR OF ABOVE CODE FOR RIGHT SUBTREE Note that we reach here only when node was not found in left subtree ; If target was neither present in left nor in right subtree ; Driver program to test the above functions ; Let us construct the tree shown in above diagram
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printkdistanceNodeDown ( Node node , int k ) { if ( node == null k < 0 ) return ; if ( k == 0 ) { System . out . print ( node . data ) ; System . out . println ( " " ) ; return ; } printkdistanceNodeDown ( node . left , k - 1 ) ; printkdistanceNodeDown ( node . right , k - 1 ) ; } int printkdistanceNode ( Node node , Node target , int k ) { if ( node == null ) return - 1 ; if ( node == target ) { printkdistanceNodeDown ( node , k ) ; return 0 ; } int dl = printkdistanceNode ( node . left , target , k ) ; if ( dl != - 1 ) { if ( dl + 1 == k ) { System . out . print ( node . data ) ; System . out . println ( " " ) ; } else printkdistanceNodeDown ( node . right , k - dl - 2 ) ; return 1 + dl ; } int dr = printkdistanceNode ( node . right , target , k ) ; if ( dr != - 1 ) { if ( dr + 1 == k ) { System . out . print ( node . data ) ; System . out . println ( " " ) ; } else printkdistanceNodeDown ( node . left , k - dr - 2 ) ; return 1 + dr ; } return - 1 ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 22 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; Node target = tree . root . left . right ; tree . printkdistanceNode ( tree . root , target , 2 ) ; } }
Print all nodes that don 't have sibling | A binary tree node ; Function to print all non - root nodes that don 't have a sibling ; Base case ; If this is an internal node , recur for left and right subtrees ; If left child is NULL and right is not , print right child and recur for right child ; If right child is NULL and left is not , print left child and recur for left child ; Driver program to test the above functions ; Let us construct the tree shown in above diagram
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printSingles ( Node node ) { if ( node == null ) return ; if ( node . left != null && node . right != null ) { printSingles ( node . left ) ; printSingles ( node . right ) ; } else if ( node . right != null ) { System . out . print ( node . right . data + " ▁ " ) ; printSingles ( node . right ) ; } else if ( node . left != null ) { System . out . print ( node . left . data + " ▁ " ) ; printSingles ( node . left ) ; } } 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 . right = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . left . right = new Node ( 6 ) ; tree . printSingles ( tree . root ) ; } }
Print all nodes that are at distance k from a leaf node | Java program to print all nodes at a distance k from leaf A binary tree node ; This function prints all nodes that are distance k from a leaf node path [ ] -- > Store ancestors of a node visited [ ] -- > Stores true if a node is printed as output . A node may be k distance away from many leaves , we want to print it once ; Base case ; append this Node to the path array ; it 's a leaf, so print the ancestor at distance k only  if the ancestor is not already printed  ; If not leaf node , recur for left and right subtrees ; Given a binary tree and a nuber k , print all nodes that are k distant from a leaf ; Driver program to test the above functions ; Let us construct the tree shown in above diagram
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void kDistantFromLeafUtil ( Node node , int path [ ] , boolean visited [ ] , int pathLen , int k ) { if ( node == null ) return ; path [ pathLen ] = node . data ; visited [ pathLen ] = false ; pathLen ++ ; if ( node . left == null && node . right == null && pathLen - k - 1 >= 0 && visited [ pathLen - k - 1 ] == false ) { System . out . print ( path [ pathLen - k - 1 ] + " ▁ " ) ; visited [ pathLen - k - 1 ] = true ; return ; } kDistantFromLeafUtil ( node . left , path , visited , pathLen , k ) ; kDistantFromLeafUtil ( node . right , path , visited , pathLen , k ) ; } void printKDistantfromLeaf ( Node node , int k ) { int path [ ] = new int [ 1000 ] ; boolean visited [ ] = new boolean [ 1000 ] ; kDistantFromLeafUtil ( node , path , visited , 0 , k ) ; } 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 ) ; tree . root . right . left . right = new Node ( 8 ) ; System . out . println ( " ▁ Nodes ▁ at ▁ distance ▁ 2 ▁ are ▁ : " ) ; tree . printKDistantfromLeaf ( tree . root , 2 ) ; } }
Print all nodes that are at distance k from a leaf node | A binary tree node ; Given a binary tree and a nuber k , print all nodes that are k distant from a leaf ; leaf node ; parent of left leaf ; parent of right leaf ; Driver program to test the above functions ; Let us construct the tree shown in above diagram
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int printKDistantfromLeaf ( Node node , int k ) { if ( node == null ) return - 1 ; int lk = printKDistantfromLeaf ( node . left , k ) ; int rk = printKDistantfromLeaf ( node . right , k ) ; boolean isLeaf = lk == - 1 && lk == rk ; if ( lk == 0 || rk == 0 || ( isLeaf && k == 0 ) ) System . out . print ( " ▁ " + node . data ) ; if ( isLeaf && k > 0 ) return k - 1 ; if ( lk > 0 && lk < k ) return lk - 1 ; if ( rk > 0 && rk < k ) return rk - 1 ; return - 2 ; } 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 ) ; tree . root . right . left . right = new Node ( 8 ) ; System . out . println ( " ▁ Nodes ▁ at ▁ distance ▁ 2 ▁ are ▁ : " ) ; tree . printKDistantfromLeaf ( tree . root , 2 ) ; } }
Print leftmost and rightmost nodes of a Binary Tree | Java program to print corner node at each level in a binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; Function to print corner node at each level ; star node is for keeping track of levels ; pushing root node and star node ; Do level order traversal of Binary Tree ; n is the no of nodes in current Level ; If it is leftmost corner value or rightmost corner value then print it ; push the left and right children of the temp node ; Driver program to test above functions
import java . util . * ; class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; void printCorner ( Node root ) { Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { int n = q . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Node temp = q . peek ( ) ; q . poll ( ) ; if ( i == 0 i == n - 1 ) System . out . print ( temp . key + " ▁ " ) ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 15 ) ; tree . root . left = new Node ( 10 ) ; tree . root . right = new Node ( 20 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . right . left = new Node ( 16 ) ; tree . root . right . right = new Node ( 25 ) ; tree . printCorner ( tree . root ) ; } }
Print Binary Tree in 2 | Java Program to print binary tree in 2D ; A binary tree node ; Constructor that allocates a new node with the given data and null left and right pointers . ; Function to print binary tree in 2D It does reverse inorder traversal ; Base case ; Increase distance between levels ; Process right child first ; Print current node after space count ; Process left child ; Wrapper over print2DUtil ( ) ; Pass initial space count as 0 ; Driver code
class GFG { static final int COUNT = 10 ; static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } ; static void print2DUtil ( Node root , int space ) { if ( root == null ) return ; space += COUNT ; print2DUtil ( root . right , space ) ; System . out . print ( "NEW_LINE"); for ( int i = COUNT ; i < space ; i ++ ) System . out . print ( " ▁ " ) ; System . out . print ( root . data + "NEW_LINE"); print2DUtil ( root . left , space ) ; } static void print2D ( Node root ) { print2DUtil ( root , 0 ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; 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 . left = new Node ( 12 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; root . right . right . right = new Node ( 15 ) ; print2D ( root ) ; } }
Print Left View of a Binary Tree | Class containing left and right child of current node and key value ; Class to print the left view ; recursive function to print left view ; Base Case ; If this is the first node of its level ; Recur for left and right subtrees ; A wrapper over leftViewUtil ( ) ; testing for example nodes
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; static int max_level = 0 ; void leftViewUtil ( Node node , int level ) { if ( node == null ) return ; if ( max_level < level ) { System . out . print ( " ▁ " + node . data ) ; max_level = level ; } leftViewUtil ( node . left , level + 1 ) ; leftViewUtil ( node . right , level + 1 ) ; } void leftView ( ) { leftViewUtil ( root , 1 ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 12 ) ; tree . root . left = new Node ( 10 ) ; tree . root . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 25 ) ; tree . root . right . right = new Node ( 40 ) ; tree . leftView ( ) ; } }
Print Left View of a Binary Tree | Java program to print left view of Binary Tree ; Binary tree node ; function to print left view of binary tree ; number of nodes at current level ; Traverse all nodes of current level ; Print the left most element at the level ; Add left node to queue ; Add right node to queue ; Driver code ; construct binary tree as shown in above diagram
import java . util . * ; public class PrintRightView { private static class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } private static void printLeftView ( Node root ) { if ( root == null ) return ; Queue < Node > queue = new LinkedList < > ( ) ; queue . add ( root ) ; while ( ! queue . isEmpty ( ) ) { int n = queue . size ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { Node temp = queue . poll ( ) ; if ( i == 1 ) System . out . print ( temp . data + " ▁ " ) ; if ( temp . left != null ) queue . add ( temp . left ) ; if ( temp . right != null ) queue . add ( temp . right ) ; } } } public static void main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 7 ) ; root . left . right = new Node ( 8 ) ; root . right . right = new Node ( 15 ) ; root . right . left = new Node ( 12 ) ; root . right . right . left = new Node ( 14 ) ; printLeftView ( root ) ; } }
Reduce the given Array of [ 1 , N ] by rotating left or right based on given conditions | Java program for the above approach ; Function to find the last element left after performing N - 1 queries of type X ; Stores the next power of 2 ; Iterate until nextPower is at most N ; If X is equal to 1 ; Stores the power of 2 less than or equal to N ; Return the final value ; Driver Code
import java . io . * ; class GFG { static int rotate ( int arr [ ] , int N , int X ) { long nextPower = 1 ; while ( nextPower <= N ) nextPower *= 2 ; if ( X == 1 ) return ( int ) nextPower - N ; long prevPower = nextPower / 2 ; return 2 * ( N - ( int ) prevPower ) + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int X = 1 ; int N = arr . length ; System . out . println ( rotate ( arr , N , X ) ) ; } }
Maximum number of 0 s placed consecutively at the start and end in any rotation of a Binary String | Java program for the above approach ; Function to find the maximum sum of consecutive 0 s present at the start and end of a string present in any of the rotations of the given string ; Check if all the characters in the string are 0 ; Iterate over characters of the string ; If the frequency of '1' is 0 ; Print n as the result ; Concatenate the string with itself ; Stores the required result ; Generate all rotations of the string ; Store the number of consecutive 0 s at the start and end of the string ; Count 0 s present at the start ; Count 0 s present at the end ; Calculate the sum ; Update the overall maximum sum ; Print the result ; Driver Code ; Given string ; Store the size of the string
import java . util . * ; class GFG { static void findMaximumZeros ( String str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str . charAt ( i ) == '0' ) c0 ++ ; } if ( c0 == n ) { System . out . print ( n ) ; return ; } String s = str + str ; int mx = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int cs = 0 ; int ce = 0 ; for ( int j = i ; j < i + n ; ++ j ) { if ( s . charAt ( j ) == '0' ) cs ++ ; else break ; } for ( int j = i + n - 1 ; j >= i ; -- j ) { if ( s . charAt ( j ) == '0' ) ce ++ ; else break ; } int val = cs + ce ; mx = Math . max ( val , mx ) ; } System . out . print ( mx ) ; } public static void main ( String [ ] args ) { String s = "1001" ; int n = s . length ( ) ; findMaximumZeros ( s , n ) ; } }