codeparrot/xlcost-text-to-code huggingface dataset:
Check if all leaves are at same level | Python3 program to check if all leaf nodes are at same level of binary tree ; Tree Node returns a new tree Node ; return true if all leaf nodes are at same level , else false ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check for left child ; if its leaf node ; if it 's first leaf node,  then update result ; if it 's not first leaf node,  then compare the level with  level of previous leaf node ; check for right child ; if it 's leaf node ; if it 's first leaf node till now,  then update the result ; if it is not the first leaf node , then compare the level with level of previous leaf node ; Driver Code ; construct a tree
INT_MAX = 2 ** 31 NEW_LINE INT_MIN = - 2 ** 31 NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def checkLevelLeafNode ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE result = INT_MAX NEW_LINE level = 0 NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT size = len ( q ) NEW_LINE level += 1 NEW_LINE while ( size > 0 or len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE if ( not temp . left . right and not temp . left . left ) : NEW_LINE INDENT if ( result == INT_MAX ) : NEW_LINE INDENT result = level NEW_LINE DEDENT elif ( result != level ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE if ( not temp . right . left and not temp . right . right ) : NEW_LINE INDENT if ( result == INT_MAX ) : NEW_LINE INDENT result = level NEW_LINE DEDENT elif ( result != level ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT size -= 1 NEW_LINE DEDENT DEDENT DEDENT return 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 4 ) NEW_LINE root . right . left = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE result = checkLevelLeafNode ( root ) NEW_LINE if ( result ) : NEW_LINE INDENT print ( " All ▁ leaf ▁ nodes ▁ are ▁ at ▁ same ▁ level " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Leaf ▁ nodes ▁ not ▁ at ▁ same ▁ level " ) NEW_LINE DEDENT DEDENT
Sorting a Queue without extra space | Python3 program to implement sorting a queue data structure ; Queue elements after sortedIndex are already sorted . This function returns index of minimum element from front to sortedIndex ; This is dequeue ( ) in C ++ STL ; we add the condition i <= sortedIndex because we don 't want to traverse   on the sorted part of the queue,   which is the right part.  ; This is enqueue ( ) in C ++ STL ; Moves given minimum element to rear of queue ; SortQueue Function ; Driver code ; Sort queue ; Prsorted queue
from queue import Queue NEW_LINE def minIndex ( q , sortedIndex ) : NEW_LINE INDENT min_index = - 1 NEW_LINE min_val = 999999999999 NEW_LINE n = q . qsize ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr = q . queue [ 0 ] NEW_LINE q . get ( ) NEW_LINE if ( curr <= min_val and i <= sortedIndex ) : NEW_LINE INDENT min_index = i NEW_LINE min_val = curr NEW_LINE DEDENT q . put ( curr ) NEW_LINE DEDENT return min_index NEW_LINE DEDENT def insertMinToRear ( q , min_index ) : NEW_LINE INDENT min_val = None NEW_LINE n = q . qsize ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr = q . queue [ 0 ] NEW_LINE q . get ( ) NEW_LINE if ( i != min_index ) : NEW_LINE INDENT q . put ( curr ) NEW_LINE DEDENT else : NEW_LINE INDENT min_val = curr NEW_LINE DEDENT DEDENT q . put ( min_val ) NEW_LINE DEDENT def sortQueue ( q ) : NEW_LINE INDENT for i in range ( 1 , q . qsize ( ) + 1 ) : NEW_LINE INDENT min_index = minIndex ( q , q . qsize ( ) - i ) NEW_LINE insertMinToRear ( q , min_index ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = Queue ( ) NEW_LINE q . put ( 30 ) NEW_LINE q . put ( 11 ) NEW_LINE q . put ( 15 ) NEW_LINE q . put ( 4 ) NEW_LINE sortQueue ( q ) NEW_LINE while ( q . empty ( ) == False ) : NEW_LINE INDENT print ( q . queue [ 0 ] , end = " ▁ " ) NEW_LINE q . get ( ) NEW_LINE DEDENT DEDENT
Check if removing an edge can divide a Binary Tree in two halves | Python3 program to check if there exist an edge whose removal creates two trees of same size utility function to create a new node ; To calculate size of tree with given root ; This function returns true if there is an edge whose removal can divide the tree in two halves n is size of tree ; Base cases ; Check for root ; Check for rest of the nodes ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Now recursively check all nodes ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def count ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( count ( root . left ) + count ( root . right ) + 1 ) NEW_LINE DEDENT def checkRec ( root , n ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( count ( root ) == n - count ( root ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return ( checkRec ( root . left , n ) or checkRec ( root . right , n ) ) NEW_LINE DEDENT def check ( root ) : NEW_LINE INDENT n = count ( root ) NEW_LINE return checkRec ( root , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 5 ) NEW_LINE root . left = newNode ( 1 ) NEW_LINE root . right = newNode ( 6 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 4 ) NEW_LINE if check ( root ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Sliding Window Maximum ( Maximum of all subarrays of size k ) | Python program to find the maximum for each and every contiguous subarray of size k ; A Deque ( Double ended queue ) based method for printing maximum element of all subarrays of size k ; Create a Double Ended Queue , Qi that will store indexes of array elements . The queue will store indexes of useful elements in every window and it will maintain decreasing order of values from front to rear in Qi , i . e . , arr [ Qi . front [ ] ] to arr [ Qi . rear ( ) ] are sorted in decreasing order ; Process first k ( or first window ) elements of array ; For every element , the previous smaller elements are useless so remove them from Qi ; Remove from rear ; Add new element at rear of queue ; Process rest of the elements , i . e . from arr [ k ] to arr [ n - 1 ] ; The element at the front of the queue is the largest element of previous window , so print it ; Remove the elements which are out of this window ; remove from front of deque ; Remove all elements smaller than the currently being added element ( Remove useless elements ) ; Add current element at the rear of Qi ; Print the maximum element of last window ; Driver code
from collections import deque NEW_LINE def printMax ( arr , n , k ) : NEW_LINE INDENT Qi = deque ( ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT while Qi and arr [ i ] >= arr [ Qi [ - 1 ] ] : NEW_LINE INDENT Qi . pop ( ) NEW_LINE DEDENT Qi . append ( i ) ; NEW_LINE DEDENT for i in range ( k , n ) : NEW_LINE INDENT print ( str ( arr [ Qi [ 0 ] ] ) + " ▁ " , end = " " ) NEW_LINE while Qi and Qi [ 0 ] <= i - k : NEW_LINE INDENT Qi . popleft ( ) NEW_LINE DEDENT while Qi and arr [ i ] >= arr [ Qi [ - 1 ] ] : NEW_LINE INDENT Qi . pop ( ) NEW_LINE DEDENT Qi . append ( i ) NEW_LINE DEDENT print ( str ( arr [ Qi [ 0 ] ] ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 1 , 78 , 90 , 57 , 89 , 56 ] NEW_LINE k = 3 NEW_LINE printMax ( arr , len ( arr ) , k ) NEW_LINE DEDENT
Sum of minimum and maximum elements of all subarrays of size k . | Python3 program to find Sum of all minimum and maximum elements Of Sub - array Size k . ; Returns Sum of min and max element of all subarrays of size k ; Initialize result ; The queue will store indexes of useful elements in every window In deque ' G ' we maintain decreasing order of values from front to rear In deque ' S ' we maintain increasing order of values from front to rear ; Process first window of size K ; Remove all previous greater elements that are useless . ; Remove from rear ; Remove all previous smaller that are elements are useless . ; Remove from rear ; Add current element at rear of both deque ; Process rest of the Array elements ; Element at the front of the deque ' G ' & ' S ' is the largest and smallest element of previous window respectively ; Remove all elements which are out of this window ; remove all previous greater element that are useless ; Remove from rear ; remove all previous smaller that are elements are useless ; Remove from rear ; Add current element at rear of both deque ; Sum of minimum and maximum element of last window ; Driver program to test above functions
from collections import deque NEW_LINE def SumOfKsubArray ( arr , n , k ) : NEW_LINE INDENT Sum = 0 NEW_LINE S = deque ( ) NEW_LINE G = deque ( ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT while ( len ( S ) > 0 and arr [ S [ - 1 ] ] >= arr [ i ] ) : NEW_LINE INDENT S . pop ( ) NEW_LINE DEDENT while ( len ( G ) > 0 and arr [ G [ - 1 ] ] <= arr [ i ] ) : NEW_LINE INDENT G . pop ( ) NEW_LINE DEDENT G . append ( i ) NEW_LINE S . append ( i ) NEW_LINE DEDENT for i in range ( k , n ) : NEW_LINE INDENT Sum += arr [ S [ 0 ] ] + arr [ G [ 0 ] ] NEW_LINE while ( len ( S ) > 0 and S [ 0 ] <= i - k ) : NEW_LINE INDENT S . popleft ( ) NEW_LINE DEDENT while ( len ( G ) > 0 and G [ 0 ] <= i - k ) : NEW_LINE INDENT G . popleft ( ) NEW_LINE DEDENT while ( len ( S ) > 0 and arr [ S [ - 1 ] ] >= arr [ i ] ) : NEW_LINE INDENT S . pop ( ) NEW_LINE DEDENT while ( len ( G ) > 0 and arr [ G [ - 1 ] ] <= arr [ i ] ) : NEW_LINE INDENT G . pop ( ) NEW_LINE DEDENT G . append ( i ) NEW_LINE S . append ( i ) NEW_LINE DEDENT Sum += arr [ S [ 0 ] ] + arr [ G [ 0 ] ] NEW_LINE return Sum NEW_LINE DEDENT arr = [ 2 , 5 , - 1 , 7 , - 3 , - 1 , - 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( SumOfKsubArray ( arr , n , k ) ) NEW_LINE
Check if removing an edge can divide a Binary Tree in two halves | Python3 program to check if there exist an edge whose removal creates two trees of same size ; To calculate size of tree with given root ; This function returns size of tree rooted with given root . It also set " res " as true if there is an edge whose removal divides tree in two halves . n is size of tree ; Base case ; Compute sizes of left and right children ; If required property is true for current node set " res " as true ; Return size ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Initialize result and recursively check all nodes bool res = false ; ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . key = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def count ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( count ( node . left ) + count ( node . right ) + 1 ) NEW_LINE DEDENT def checkRec ( root , n ) : NEW_LINE INDENT global res NEW_LINE if ( root == None ) : NEW_LINE return 0 NEW_LINE c = ( checkRec ( root . left , n ) + 1 + checkRec ( root . right , n ) ) NEW_LINE if ( c == n - c ) : NEW_LINE INDENT res = True NEW_LINE DEDENT return c NEW_LINE DEDENT def check ( root ) : NEW_LINE INDENT n = count ( root ) NEW_LINE checkRec ( root , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT res = False NEW_LINE root = Node ( 5 ) NEW_LINE root . left = Node ( 1 ) NEW_LINE root . right = Node ( 6 ) NEW_LINE root . left . left = Node ( 3 ) NEW_LINE root . right . left = Node ( 7 ) NEW_LINE root . right . right = Node ( 4 ) NEW_LINE check ( root ) NEW_LINE if res : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Distance of nearest cell having 1 in a binary matrix | Prthe distance of nearest cell having 1 for each cell . ; Initialize the answer matrix with INT_MAX . ; For each cell ; Traversing the whole matrix to find the minimum distance . ; If cell contain 1 , check for minimum distance . ; Printing the answer . ; Driver Code
def printDistance ( mat ) : NEW_LINE INDENT global N , M NEW_LINE ans = [ [ None ] * M for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT ans [ i ] [ j ] = 999999999999 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT for k in range ( N ) : NEW_LINE INDENT for l in range ( M ) : NEW_LINE INDENT if ( mat [ k ] [ l ] == 1 ) : NEW_LINE INDENT ans [ i ] [ j ] = min ( ans [ i ] [ j ] , abs ( i - k ) + abs ( j - l ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT print ( ans [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT N = 3 NEW_LINE M = 4 NEW_LINE mat = [ [ 0 , 0 , 0 , 1 ] , [ 0 , 0 , 1 , 1 ] , [ 0 , 1 , 1 , 0 ] ] NEW_LINE printDistance ( mat ) NEW_LINE
Distance of nearest cell having 1 in a binary matrix | Python3 program to find distance of nearest cell having 1 in a binary matrix . ; Making a class of graph with bfs function . ; Function to create graph with N * M nodes considering each cell as a node and each boundary as an edge . ; A number to be assigned to a cell ; If last row , then add edge on right side . ; If not bottom right cell . ; If last column , then add edge toward down . ; Else makes an edge in all four directions . ; BFS function to find minimum distance ; Printing the solution . ; Find minimum distance ; Creating a graph with nodes values assigned from 1 to N x M and matrix adjacent . ; To store minimum distance ; To mark each node as visited or not in BFS ; Initialising the value of distance and visit . ; Inserting nodes whose value in matrix is 1 in the queue . ; Calling for Bfs with given Queue . ; Printing the solution . ; Driver code
from collections import deque NEW_LINE MAX = 500 NEW_LINE N = 3 NEW_LINE M = 4 NEW_LINE g = [ [ ] for i in range ( MAX ) ] NEW_LINE n , m = 0 , 0 NEW_LINE def createGraph ( ) : NEW_LINE INDENT global g , n , m NEW_LINE k = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT if ( j != m ) : NEW_LINE INDENT g [ k ] . append ( k + 1 ) NEW_LINE g [ k + 1 ] . append ( k ) NEW_LINE DEDENT DEDENT elif ( j == m ) : NEW_LINE INDENT g [ k ] . append ( k + m ) NEW_LINE g [ k + m ] . append ( k ) NEW_LINE DEDENT else : NEW_LINE INDENT g [ k ] . append ( k + 1 ) NEW_LINE g [ k + 1 ] . append ( k ) NEW_LINE g [ k ] . append ( k + m ) NEW_LINE g [ k + m ] . append ( k ) NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT DEDENT def bfs ( visit , dist , q ) : NEW_LINE INDENT global g NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT temp = q . popleft ( ) NEW_LINE for i in g [ temp ] : NEW_LINE INDENT if ( visit [ i ] != 1 ) : NEW_LINE INDENT dist [ i ] = min ( dist [ i ] , dist [ temp ] + 1 ) NEW_LINE q . append ( i ) NEW_LINE visit [ i ] = 1 NEW_LINE DEDENT DEDENT DEDENT return dist NEW_LINE DEDENT def prt ( dist ) : NEW_LINE INDENT c = 1 NEW_LINE for i in range ( 1 , n * m + 1 ) : NEW_LINE INDENT print ( dist [ i ] , end = " ▁ " ) NEW_LINE if ( c % m == 0 ) : NEW_LINE INDENT print ( ) NEW_LINE DEDENT c += 1 NEW_LINE DEDENT DEDENT def findMinDistance ( mat ) : NEW_LINE INDENT global g , n , m NEW_LINE n , m = N , M NEW_LINE createGraph ( ) NEW_LINE dist = [ 0 ] * MAX NEW_LINE visit = [ 0 ] * MAX NEW_LINE for i in range ( 1 , M * N + 1 ) : NEW_LINE INDENT dist [ i ] = 10 ** 9 NEW_LINE visit [ i ] = 0 NEW_LINE DEDENT k = 1 NEW_LINE q = deque ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT dist [ k ] = 0 NEW_LINE visit [ k ] = 1 NEW_LINE q . append ( k ) NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT dist = bfs ( visit , dist , q ) NEW_LINE prt ( dist ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 0 , 0 , 0 , 1 ] , [ 0 , 0 , 1 , 1 ] , [ 0 , 1 , 1 , 0 ] ] NEW_LINE findMinDistance ( mat ) NEW_LINE DEDENT
First negative integer in every window of size k | Function to find the first negative integer in every window of size k ; Loop for each subarray ( window ) of size k ; Traverse through the current window ; If a negative integer is found , then it is the first negative integer for current window . Print it , set the flag and break ; If the current window does not contain a negative integer ; Driver Code
def printFirstNegativeInteger ( arr , n , k ) : NEW_LINE INDENT for i in range ( 0 , ( n - k + 1 ) ) : NEW_LINE INDENT flag = False NEW_LINE for j in range ( 0 , k ) : NEW_LINE INDENT if ( arr [ i + j ] < 0 ) : NEW_LINE INDENT print ( arr [ i + j ] , end = " ▁ " ) NEW_LINE flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not ( flag ) ) : NEW_LINE INDENT print ( "0" , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE printFirstNegativeInteger ( arr , n , k ) NEW_LINE
First negative integer in every window of size k | Python3 implementation to find the first negative integer in every window of size k import deque ( ) from collections ; function to find the first negative integer in every window of size k ; A Double Ended Queue , Di that will store indexes of useful array elements for the current window of size k . The useful elements are all negative integers . ; Process first k ( or first window ) elements of array ; Add current element at the rear of Di if it is a negative integer ; Process rest of the elements , i . e . , from arr [ k ] to arr [ n - 1 ] ; if the window does not have a negative integer ; if Di is not empty then the element at the front of the queue is the first negative integer of the previous window ; Remove the elements which are out of this window ; Remove from front of queue ; Add current element at the rear of Di if it is a negative integer ; Print the first negative integer of last window ; Driver Code
from collections import deque NEW_LINE def printFirstNegativeInteger ( arr , n , k ) : NEW_LINE INDENT Di = deque ( ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT Di . append ( i ) ; NEW_LINE DEDENT DEDENT for i in range ( k , n ) : NEW_LINE INDENT if ( not Di ) : NEW_LINE INDENT print ( 0 , end = ' ▁ ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ Di [ 0 ] ] , end = ' ▁ ' ) ; NEW_LINE DEDENT while Di and Di [ 0 ] <= ( i - k ) : NEW_LINE INDENT Di . popleft ( ) NEW_LINE DEDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT Di . append ( i ) ; NEW_LINE DEDENT DEDENT if not Di : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ Di [ 0 ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE printFirstNegativeInteger ( arr , n , k ) ; NEW_LINE DEDENT
First negative integer in every window of size k | Python3 code for First negative integer in every window of size k ; skip out of window and positive elements ; check if a negative element is found , otherwise use 0 ; Driver code
def printFirstNegativeInteger ( arr , k ) : NEW_LINE INDENT firstNegativeIndex = 0 NEW_LINE for i in range ( k - 1 , len ( arr ) ) : NEW_LINE INDENT while firstNegativeIndex < i and ( firstNegativeIndex <= i - k or arr [ firstNegativeIndex ] > 0 ) : NEW_LINE INDENT firstNegativeIndex += 1 NEW_LINE DEDENT firstNegativeElement = arr [ firstNegativeIndex ] if arr [ firstNegativeIndex ] < 0 else 0 NEW_LINE print ( firstNegativeElement , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 ] NEW_LINE k = 3 NEW_LINE printFirstNegativeInteger ( arr , k ) NEW_LINE DEDENT
Check if all levels of two trees are anagrams or not | Returns true if trees with root1 and root2 are level by level anagram , else returns false . ; Base Cases ; start level order traversal of two trees using two queues . ; n1 ( queue size ) indicates number of Nodes at current level in first tree and n2 indicates number of nodes in current level of second tree . ; If n1 and n2 are different ; If level order traversal is over ; Dequeue all Nodes of current level and Enqueue all Nodes of next level ; Check if nodes of current levels are anagrams or not . ; Utility function to create a new tree Node ; Driver Code ; Constructing both the trees .
def areAnagrams ( root1 , root2 ) : NEW_LINE INDENT if ( root1 == None and root2 == None ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( root1 == None or root2 == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT q1 = [ ] NEW_LINE q2 = [ ] NEW_LINE q1 . append ( root1 ) NEW_LINE q2 . append ( root2 ) NEW_LINE while ( 1 ) : NEW_LINE INDENT n1 = len ( q1 ) NEW_LINE n2 = len ( q2 ) NEW_LINE if ( n1 != n2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n1 == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT curr_level1 = [ ] NEW_LINE curr_level2 = [ ] NEW_LINE while ( n1 > 0 ) : NEW_LINE INDENT node1 = q1 [ 0 ] NEW_LINE q1 . pop ( 0 ) NEW_LINE if ( node1 . left != None ) : NEW_LINE INDENT q1 . append ( node1 . left ) NEW_LINE DEDENT if ( node1 . right != None ) : NEW_LINE INDENT q1 . append ( node1 . right ) NEW_LINE DEDENT n1 -= 1 NEW_LINE node2 = q2 [ 0 ] NEW_LINE q2 . pop ( 0 ) NEW_LINE if ( node2 . left != None ) : NEW_LINE INDENT q2 . append ( node2 . left ) NEW_LINE DEDENT if ( node2 . right != None ) : NEW_LINE INDENT q2 . append ( node2 . right ) NEW_LINE DEDENT curr_level1 . append ( node1 . data ) NEW_LINE curr_level2 . append ( node2 . data ) NEW_LINE DEDENT curr_level1 . sort ( ) NEW_LINE curr_level2 . sort ( ) NEW_LINE if ( curr_level1 != curr_level2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = newNode ( 1 ) NEW_LINE root1 . left = newNode ( 3 ) NEW_LINE root1 . right = newNode ( 2 ) NEW_LINE root1 . right . left = newNode ( 5 ) NEW_LINE root1 . right . right = newNode ( 4 ) NEW_LINE root2 = newNode ( 1 ) NEW_LINE root2 . left = newNode ( 2 ) NEW_LINE root2 . right = newNode ( 3 ) NEW_LINE root2 . left . left = newNode ( 4 ) NEW_LINE root2 . left . right = newNode ( 5 ) NEW_LINE if areAnagrams ( root1 , root2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT