codeparrot/xlcost-text-to-code huggingface dataset:
Lowest Common Ancestor in a Binary Search Tree . | A recursive python program to find LCA of two nodes n1 and n2 A Binary tree node ; Function to find LCA of n1 and n2 . The function assumes that both n1 and n2 are present in BST ; If both n1 and n2 are smaller than root , then LCA lies in left ; If both n1 and n2 are greater than root , then LCA lies in right ; Let us construct the BST shown in the figure
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def lca ( root , n1 , n2 ) : NEW_LINE INDENT while root : NEW_LINE INDENT if root . data > n1 and root . data > n2 : NEW_LINE INDENT root = root . left NEW_LINE DEDENT elif root . data < n1 and root . data < n2 : NEW_LINE INDENT root = root . right NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return root NEW_LINE DEDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 22 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 12 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 14 ) NEW_LINE n1 = 10 ; n2 = 14 NEW_LINE t = lca ( root , n1 , n2 ) NEW_LINE print " LCA ▁ of ▁ % d ▁ and ▁ % d ▁ is ▁ % d " % ( n1 , n2 , t . data ) NEW_LINE n1 = 14 ; n2 = 8 NEW_LINE t = lca ( root , n1 , n2 ) NEW_LINE print " LCA ▁ of ▁ % d ▁ and ▁ % d ▁ is ▁ % d " % ( n1 , n2 , t . data ) NEW_LINE n1 = 10 ; n2 = 22 NEW_LINE t = lca ( root , n1 , n2 ) NEW_LINE print " LCA ▁ of ▁ % d ▁ and ▁ % d ▁ is ▁ % d " % ( n1 , n2 , t . data ) NEW_LINE
A program to check if a binary tree is BST or not |  ; Helper function that allocates a new node with the given data and None left and right poers . ; Returns true if given tree is BST . ; Base condition ; if left node exist then check it has correct data or not i . e . left node ' s ▁ data ▁ ▁ should ▁ be ▁ less ▁ than ▁ root ' s data ; if right node exist then check it has correct data or not i . e . right node ' s ▁ data ▁ ▁ should ▁ be ▁ greater ▁ than ▁ root ' s data ; check recursively for every node . ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isBST ( root , l = None , r = None ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( l != None and root . data <= l . data ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( r != None and root . data >= r . data ) : NEW_LINE INDENT return False NEW_LINE DEDENT return isBST ( root . left , l , root ) and isBST ( root . right , root , r ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 3 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 1 ) NEW_LINE root . right . right = newNode ( 4 ) NEW_LINE if ( isBST ( root , None , None ) ) : NEW_LINE INDENT print ( " Is ▁ BST " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ BST " ) NEW_LINE DEDENT DEDENT
A program to check if a binary tree is BST or not | Python3 program to check if a given tree is BST . ; A binary tree node has data , pointer to left child and a pointer to right child ; traverse the tree in inorder fashion and keep track of prev node ; Allows only distinct valued nodes ; Driver Code
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isBSTUtil ( root , prev ) : NEW_LINE INDENT if ( root != None ) : NEW_LINE INDENT if ( isBSTUtil ( root . left , prev ) == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( prev != None and root . data <= prev . data ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev = root NEW_LINE return isBSTUtil ( root . right , prev ) NEW_LINE DEDENT return True NEW_LINE DEDENT def isBST ( root ) : NEW_LINE INDENT prev = None NEW_LINE return isBSTUtil ( root , prev ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 3 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 1 ) NEW_LINE root . right . right = Node ( 4 ) NEW_LINE if ( isBST ( root ) == None ) : NEW_LINE INDENT print ( " Is ▁ BST " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ BST " ) NEW_LINE DEDENT DEDENT
Find k | A BST node ; Recursive function to insert an key into BST ; Function to find k 'th largest element in BST. Here count denotes the number of nodes processed so far ; Base case ; Search in left subtree ; If k 'th smallest is found in  left subtree, return it ; If current element is k 'th  smallest, return it ; Else search in right subtree ; Function to find k 'th largest element in BST ; Maintain index to count number of nodes processed so far ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( root , x ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return Node ( x ) NEW_LINE DEDENT if ( x < root . data ) : NEW_LINE INDENT root . left = insert ( root . left , x ) NEW_LINE DEDENT elif ( x > root . data ) : NEW_LINE INDENT root . right = insert ( root . right , x ) NEW_LINE DEDENT return root NEW_LINE DEDENT def kthSmallest ( root ) : NEW_LINE INDENT global k NEW_LINE if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT left = kthSmallest ( root . left ) NEW_LINE if ( left != None ) : NEW_LINE INDENT return left NEW_LINE DEDENT k -= 1 NEW_LINE if ( k == 0 ) : NEW_LINE INDENT return root NEW_LINE DEDENT return kthSmallest ( root . right ) NEW_LINE DEDENT def printKthSmallest ( root ) : NEW_LINE INDENT count = 0 NEW_LINE res = kthSmallest ( root ) NEW_LINE if ( res == None ) : NEW_LINE INDENT print ( " There ▁ are ▁ less ▁ than ▁ k ▁ nodes ▁ in ▁ the ▁ BST " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " K - th ▁ Smallest ▁ Element ▁ is ▁ " , res . data ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE keys = [ 20 , 8 , 22 , 4 , 12 , 10 , 14 ] NEW_LINE for x in keys : NEW_LINE INDENT root = insert ( root , x ) NEW_LINE DEDENT k = 3 NEW_LINE printKthSmallest ( root ) NEW_LINE DEDENT
Find k | A BST node ; Recursive function to insert an key into BST ; If a node is inserted in left subtree , then lCount of this node is increased . For simplicity , we are assuming that all keys ( tried to be inserted ) are distinct . ; Function to find k 'th largest element in BST. Here count denotes the number of nodes processed so far ; Base case ; Else search in right subtree ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE self . lCount = 0 NEW_LINE DEDENT DEDENT def insert ( root , x ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return newNode ( x ) NEW_LINE DEDENT if ( x < root . data ) : NEW_LINE INDENT root . left = insert ( root . left , x ) NEW_LINE root . lCount += 1 NEW_LINE DEDENT elif ( x > root . data ) : NEW_LINE INDENT root . right = insert ( root . right , x ) ; NEW_LINE DEDENT return root NEW_LINE DEDENT def kthSmallest ( root , k ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT count = root . lCount + 1 NEW_LINE if ( count == k ) : NEW_LINE INDENT return root NEW_LINE DEDENT if ( count > k ) : NEW_LINE INDENT return kthSmallest ( root . left , k ) NEW_LINE DEDENT return kthSmallest ( root . right , k - count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE keys = [ 20 , 8 , 22 , 4 , 12 , 10 , 14 ] NEW_LINE for x in keys : NEW_LINE INDENT root = insert ( root , x ) NEW_LINE DEDENT k = 4 NEW_LINE res = kthSmallest ( root , k ) NEW_LINE if ( res == None ) : NEW_LINE INDENT print ( " There ▁ are ▁ less ▁ than ▁ k ▁ nodes ▁ in ▁ the ▁ BST " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " K - th ▁ Smallest ▁ Element ▁ is " , res . data ) NEW_LINE DEDENT DEDENT
Construct Tree from given Inorder and Preorder traversals | A binary tree node has data , poer to left child and a poer to right child ; Recursive function to construct binary of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't do any error checking for cases where inorder and preorder do not form a tree ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; This function mainly creates an unordered_map , then calls buildTree ( ) ; This funtcion is here just to test buildTree ( ) ; Driver code ; Let us test the built tree by pring Insorder traversal
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def buildTree ( inn , pre , inStrt , inEnd ) : NEW_LINE INDENT global preIndex , mp NEW_LINE if ( inStrt > inEnd ) : NEW_LINE INDENT return None NEW_LINE DEDENT curr = pre [ preIndex ] NEW_LINE preIndex += 1 NEW_LINE tNode = Node ( curr ) NEW_LINE if ( inStrt == inEnd ) : NEW_LINE INDENT return tNode NEW_LINE DEDENT inIndex = mp [ curr ] NEW_LINE tNode . left = buildTree ( inn , pre , inStrt , inIndex - 1 ) NEW_LINE tNode . right = buildTree ( inn , pre , inIndex + 1 , inEnd ) NEW_LINE return tNode NEW_LINE DEDENT def buldTreeWrap ( inn , pre , lenn ) : NEW_LINE INDENT global mp NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT mp [ inn [ i ] ] = i NEW_LINE DEDENT return buildTree ( inn , pre , 0 , lenn - 1 ) NEW_LINE DEDENT def prInorder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT prInorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE prInorder ( node . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mp = { } NEW_LINE preIndex = 0 NEW_LINE inn = [ ' D ' , ' B ' , ' E ' , ' A ' , ' F ' , ' C ' ] NEW_LINE pre = [ ' A ' , ' B ' , ' D ' , ' E ' , ' C ' , ' F ' ] NEW_LINE lenn = len ( inn ) NEW_LINE root = buldTreeWrap ( inn , pre , lenn ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ " " the ▁ constructed ▁ tree ▁ is " ) NEW_LINE prInorder ( root ) NEW_LINE DEDENT
Check if each internal node of a BST has exactly one child | Check if each internal node of BST has only one child ; driver program to test above function
def hasOnlyOneChild ( pre , size ) : NEW_LINE INDENT nextDiff = 0 ; lastDiff = 0 NEW_LINE for i in range ( size - 1 ) : NEW_LINE INDENT nextDiff = pre [ i ] - pre [ i + 1 ] NEW_LINE lastDiff = pre [ i ] - pre [ size - 1 ] NEW_LINE if nextDiff * lastDiff < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT pre = [ 8 , 3 , 5 , 7 , 6 ] NEW_LINE size = len ( pre ) NEW_LINE if ( hasOnlyOneChild ( pre , size ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if each internal node of a BST has exactly one child | Check if each internal node of BST has only one child approach 2 ; Initialize min and max using last two elements ; Every element must be either smaller than min or greater than max ; Driver program to test above function
def hasOnlyOneChild ( pre , size ) : NEW_LINE INDENT min = 0 ; max = 0 NEW_LINE if pre [ size - 1 ] > pre [ size - 2 ] : NEW_LINE INDENT max = pre [ size - 1 ] NEW_LINE min = pre [ size - 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT max = pre [ size - 2 ] NEW_LINE min = pre [ size - 1 ] NEW_LINE DEDENT for i in range ( size - 3 , 0 , - 1 ) : NEW_LINE INDENT if pre [ i ] < min : NEW_LINE INDENT min = pre [ i ] NEW_LINE DEDENT elif pre [ i ] > max : NEW_LINE INDENT max = pre [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT pre = [ 8 , 3 , 5 , 7 , 6 ] NEW_LINE size = len ( pre ) NEW_LINE if ( hasOnlyOneChild ( pre , size ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
K 'th smallest element in BST using O(1) Extra Space | A BST node ; A function to find ; Count to iterate over elements till we get the kth smallest number ; store the Kth smallest ; to store the current node ; Like Morris traversal if current does not have left child rather than printing as we did in inorder , we will just increment the count as the number will be in an increasing order ; if count is equal to K then we found the kth smallest , so store it in ksmall ; go to current 's right child ; we create links to Inorder Successor and count using these links ; building links ; link made to Inorder Successor ; While breaking the links in so made temporary threaded tree we will check for the K smallest condition ; Revert the changes made in if part ( break link from the Inorder Successor ) ; If count is equal to K then we found the kth smallest and so store it in ksmall ; return the found value ; A utility function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Driver Code ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . key = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def KSmallestUsingMorris ( root , k ) : NEW_LINE INDENT count = 0 NEW_LINE ksmall = - 9999999999 NEW_LINE curr = root NEW_LINE while curr != None : NEW_LINE INDENT if curr . left == None : NEW_LINE INDENT count += 1 NEW_LINE if count == k : NEW_LINE INDENT ksmall = curr . key NEW_LINE DEDENT curr = curr . right NEW_LINE DEDENT else : NEW_LINE INDENT pre = curr . left NEW_LINE while ( pre . right != None and pre . right != curr ) : NEW_LINE INDENT pre = pre . right NEW_LINE DEDENT if pre . right == None : NEW_LINE INDENT pre . right = curr NEW_LINE curr = curr . left NEW_LINE DEDENT else : NEW_LINE INDENT pre . right = None NEW_LINE count += 1 NEW_LINE if count == k : NEW_LINE INDENT ksmall = curr . key NEW_LINE DEDENT curr = curr . right NEW_LINE DEDENT DEDENT DEDENT return ksmall NEW_LINE DEDENT def insert ( node , key ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return Node ( key ) NEW_LINE DEDENT if key < node . key : NEW_LINE INDENT node . left = insert ( node . left , key ) NEW_LINE DEDENT elif key > node . key : NEW_LINE INDENT node . right = insert ( node . right , key ) NEW_LINE DEDENT return node NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 50 ) NEW_LINE insert ( root , 30 ) NEW_LINE insert ( root , 20 ) NEW_LINE insert ( root , 40 ) NEW_LINE insert ( root , 70 ) NEW_LINE insert ( root , 60 ) NEW_LINE insert ( root , 80 ) NEW_LINE for k in range ( 1 , 8 ) : NEW_LINE INDENT print ( KSmallestUsingMorris ( root , k ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Check if given sorted sub | Constructor to create a new node ; A utility function to insert a given key to BST ; function to check if given sorted sub - sequence exist in BST index . iterator for given sorted sub - sequence seq [ ] . given sorted sub - sequence ; We traverse left subtree first in Inorder ; If current node matches with se [ index [ 0 ] ] then move forward in sub - sequence ; We traverse left subtree in the end in Inorder ; A wrapper over seqExistUtil . It returns true if seq [ 0. . n - 1 ] exists in tree . ; Initialize index in seq [ ] ; Do an inorder traversal and find if all elements of seq [ ] were present ; index would become n if all elements of seq [ ] were present ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( root , key ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return Node ( key ) NEW_LINE DEDENT if root . data > key : NEW_LINE INDENT root . left = insert ( root . left , key ) NEW_LINE DEDENT else : NEW_LINE INDENT root . right = insert ( root . right , key ) NEW_LINE DEDENT return root NEW_LINE DEDENT def seqExistUtil ( ptr , seq , index ) : NEW_LINE INDENT if ptr == None : NEW_LINE INDENT return NEW_LINE DEDENT seqExistUtil ( ptr . left , seq , index ) NEW_LINE if ptr . data == seq [ index [ 0 ] ] : NEW_LINE INDENT index [ 0 ] += 1 NEW_LINE DEDENT seqExistUtil ( ptr . right , seq , index ) NEW_LINE DEDENT def seqExist ( root , seq , n ) : NEW_LINE INDENT index = [ 0 ] NEW_LINE seqExistUtil ( root , seq , index ) NEW_LINE if index [ 0 ] == n : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 8 ) NEW_LINE root = insert ( root , 10 ) NEW_LINE root = insert ( root , 3 ) NEW_LINE root = insert ( root , 6 ) NEW_LINE root = insert ( root , 1 ) NEW_LINE root = insert ( root , 4 ) NEW_LINE root = insert ( root , 7 ) NEW_LINE root = insert ( root , 14 ) NEW_LINE root = insert ( root , 13 ) NEW_LINE seq = [ 4 , 6 , 8 , 14 ] NEW_LINE n = len ( seq ) NEW_LINE if seqExist ( root , seq , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if an array represents Inorder of Binary Search tree or not | Function that returns true if array is Inorder traversal of any Binary Search Tree or not . ; Array has one or no element ; Unsorted pair found ; No unsorted pair found ; Driver code
def isInorder ( arr , n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 1 , n , 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] > arr [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 19 , 23 , 25 , 30 , 45 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isInorder ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Construct Tree from given Inorder and Preorder traversals | Python3 program to construct a tree using inorder and preorder traversal ; Function to build tree using given traversal ; Function to prtree in_t Inorder ; first recur on left child ; then prthe data of node ; now recur on right child ; Driver code
class TreeNode : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . val = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT s = set ( ) NEW_LINE st = [ ] NEW_LINE def buildTree ( preorder , inorder , n ) : NEW_LINE INDENT root = None ; NEW_LINE pre = 0 NEW_LINE in_t = 0 NEW_LINE while pre < n : NEW_LINE INDENT node = None ; NEW_LINE while True : NEW_LINE INDENT node = TreeNode ( preorder [ pre ] ) NEW_LINE if ( root == None ) : NEW_LINE INDENT root = node ; NEW_LINE DEDENT if ( len ( st ) > 0 ) : NEW_LINE INDENT if ( st [ - 1 ] in s ) : NEW_LINE INDENT s . discard ( st [ - 1 ] ) ; NEW_LINE st [ - 1 ] . right = node ; NEW_LINE st . pop ( ) ; NEW_LINE DEDENT else : NEW_LINE INDENT st [ - 1 ] . left = node ; NEW_LINE DEDENT DEDENT st . append ( node ) ; NEW_LINE if pre >= n or preorder [ pre ] == inorder [ in_t ] : NEW_LINE INDENT pre += 1 NEW_LINE break NEW_LINE DEDENT pre += 1 NEW_LINE DEDENT node = None ; NEW_LINE while ( len ( st ) > 0 and in_t < n and st [ - 1 ] . val == inorder [ in_t ] ) : NEW_LINE INDENT node = st [ - 1 ] ; NEW_LINE st . pop ( ) ; NEW_LINE in_t += 1 NEW_LINE DEDENT if ( node != None ) : NEW_LINE INDENT s . add ( node ) ; NEW_LINE st . append ( node ) ; NEW_LINE DEDENT DEDENT return root ; NEW_LINE DEDENT def printInorder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return ; NEW_LINE DEDENT printInorder ( node . left ) ; NEW_LINE print ( node . val , end = " ▁ " ) ; NEW_LINE printInorder ( node . right ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT in_t = [ 9 , 8 , 4 , 2 , 10 , 5 , 10 , 1 , 6 , 3 , 13 , 12 , 7 ] NEW_LINE pre = [ 1 , 2 , 4 , 8 , 9 , 5 , 10 , 10 , 3 , 6 , 7 , 12 , 13 ] NEW_LINE l = len ( in_t ) NEW_LINE root = buildTree ( pre , in_t , l ) ; NEW_LINE printInorder ( root ) ; NEW_LINE DEDENT
Check if two BSTs contain same set of elements | BST Node ; Utility function to create Node ; function to insert elements of the tree to map m ; function to check if the two BSTs contain same set of elements ; Base cases ; Create two hash sets and store elements both BSTs in them . ; Return True if both hash sets contain same elements . ; First BST ; Second BST ; check if two BSTs have same set of elements
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . val = 0 NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def Node_ ( val1 ) : NEW_LINE INDENT temp = Node ( ) NEW_LINE temp . val = val1 NEW_LINE temp . left = temp . right = None NEW_LINE return temp NEW_LINE DEDENT s = { } NEW_LINE def insertToHash ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT insertToHash ( root . left ) NEW_LINE s . add ( root . data ) NEW_LINE insertToHash ( root . right ) NEW_LINE DEDENT def checkBSTs ( root1 , root2 ) : NEW_LINE INDENT if ( root1 != None and root2 != None ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( ( root1 == None and root2 != None ) or ( root1 != None and root2 == None ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT s1 = { } NEW_LINE s2 = { } NEW_LINE s = s1 NEW_LINE insertToHash ( root1 ) NEW_LINE s1 = s NEW_LINE s = s2 NEW_LINE insertToHash ( root2 ) NEW_LINE s2 = s NEW_LINE return ( s1 == ( s2 ) ) NEW_LINE DEDENT root1 = Node_ ( 15 ) NEW_LINE root1 . left = Node_ ( 10 ) NEW_LINE root1 . right = Node_ ( 20 ) NEW_LINE root1 . left . left = Node_ ( 5 ) NEW_LINE root1 . left . right = Node_ ( 12 ) NEW_LINE root1 . right . right = Node_ ( 25 ) NEW_LINE root2 = Node_ ( 15 ) NEW_LINE root2 . left = Node_ ( 12 ) NEW_LINE root2 . right = Node_ ( 20 ) NEW_LINE root2 . left . left = Node_ ( 5 ) NEW_LINE root2 . left . left . right = Node_ ( 10 ) NEW_LINE root2 . right . right = Node_ ( 25 ) NEW_LINE if ( checkBSTs ( root1 , root2 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT