codeparrot/xlcost-text-to-code huggingface dataset:
Count minimum substring removals required to reduce string to a single distinct character | Python3 program for the above approach ; Function to find minimum removals required to convert given string to single distinct characters only ; Unordered map to store positions of characters X , Y and Z ; Update indices of X , Y , Z ; ; Stores the count of minimum removals ; Traverse the Map ; Count the number of removals required for current character ; Update the answer ; Print the answer ; Driver Code ; Given string ; Size of string ; Function call
import sys ; NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def minimumOperations ( s , n ) : NEW_LINE INDENT mp = { } ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if s [ i ] in mp : NEW_LINE INDENT mp [ s [ i ] ] . append ( i ) ; NEW_LINE DEDENT else : NEW_LINE INDENT mp [ s [ i ] ] = [ i ] ; NEW_LINE DEDENT DEDENT ans = INT_MAX ; NEW_LINE for x in mp : NEW_LINE INDENT curr = 0 ; NEW_LINE prev = 0 ; NEW_LINE first = True ; NEW_LINE for index in mp [ x ] : NEW_LINE INDENT if ( first ) : NEW_LINE INDENT if ( index > 0 ) : NEW_LINE INDENT curr += 1 ; NEW_LINE DEDENT prev = index ; NEW_LINE first = False ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( index != prev + 1 ) : NEW_LINE INDENT curr += 1 ; NEW_LINE DEDENT prev = index ; NEW_LINE DEDENT DEDENT if ( prev != n - 1 ) : NEW_LINE INDENT curr += 1 ; NEW_LINE DEDENT ans = min ( ans , curr ) ; NEW_LINE DEDENT print ( ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " YYXYZYXYZXY " ; NEW_LINE N = len ( s ) ; NEW_LINE minimumOperations ( s , N ) ; NEW_LINE DEDENT
Program to construct DFA accepting odd number of 0 s and odd number of 1 s | Function to check whether the given is accepted by DFA or not ; Stores initial state of DFA ; Stores final state of DFA ; Stores previous state of DFA ; Iterate through the string ; Checking for all combinations ; Update the previous_state ; If final state is reached ; Otherwise ; Driver Code ; Given string ; Function Call
def checkValidDFA ( s ) : NEW_LINE INDENT initial_state = 0 NEW_LINE final_state = 0 NEW_LINE previous_state = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ( s [ i ] == '0' and previous_state == 0 ) or ( s [ i ] == '1' and previous_state == 3 ) ) : NEW_LINE INDENT final_state = 1 NEW_LINE DEDENT elif ( ( s [ i ] == '0' and previous_state == 3 ) or ( s [ i ] == '1' and previous_state == 0 ) ) : NEW_LINE INDENT final_state = 2 NEW_LINE DEDENT elif ( ( s [ i ] == '0' and previous_state == 1 ) or ( s [ i ] == '1' and previous_state == 2 ) ) : NEW_LINE INDENT final_state = 0 NEW_LINE DEDENT elif ( ( s [ i ] == '0' and previous_state == 2 ) or ( s [ i ] == '1' and previous_state == 1 ) ) : NEW_LINE INDENT final_state = 3 NEW_LINE DEDENT previous_state = final_state NEW_LINE DEDENT if ( final_state == 3 ) : NEW_LINE INDENT print ( " Accepted " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Accepted " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "010011" NEW_LINE checkValidDFA ( s ) NEW_LINE DEDENT
Difference between sums of odd level and even level nodes of a Binary Tree | Helper function that allocates a new node with the given data and None left and right poers . ; Construct to create a new 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 ; Let us create Binary Tree shown in above example
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 evenOddLevelDifference ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE level = 0 NEW_LINE evenSum = 0 NEW_LINE oddSum = 0 NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT size = len ( q ) NEW_LINE level += 1 NEW_LINE while ( size > 0 ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( level % 2 == 0 ) : NEW_LINE INDENT evenSum += temp . data NEW_LINE DEDENT else : NEW_LINE INDENT oddSum += temp . data NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT size -= 1 NEW_LINE DEDENT DEDENT return ( oddSum - evenSum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 5 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 6 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 4 ) NEW_LINE root . left . right . left = newNode ( 3 ) NEW_LINE root . right . right = newNode ( 8 ) NEW_LINE root . right . right . right = newNode ( 9 ) NEW_LINE root . right . right . left = newNode ( 7 ) NEW_LINE result = evenOddLevelDifference ( root ) NEW_LINE print ( " Diffence ▁ between ▁ sums ▁ is " , result ) NEW_LINE DEDENT
Difference between sums of odd level and even level nodes of a Binary Tree | A Binary Tree node ; The main function that returns 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 function
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 getLevelDiff ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( root . data - getLevelDiff ( root . left ) - getLevelDiff ( root . right ) ) NEW_LINE DEDENT root = Node ( 5 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 6 ) NEW_LINE root . left . left = Node ( 1 ) NEW_LINE root . left . right = Node ( 4 ) NEW_LINE root . left . right . left = Node ( 3 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE root . right . right . right = Node ( 9 ) NEW_LINE root . right . right . left = Node ( 7 ) NEW_LINE print " % d ▁ is ▁ the ▁ required ▁ difference " % ( getLevelDiff ( root ) ) NEW_LINE
Sum of all leaf nodes of binary tree | Class for node creation ; constructor ; Utility function to calculate the sum of all leaf nodes ; add root data to sum if root is a leaf node ; propagate recursively in left and right subtree ; Binary tree Fromation ; Variable to store the sum of leaf nodes
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 leafSum ( root ) : NEW_LINE INDENT global total NEW_LINE if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . left is None and root . right is None ) : NEW_LINE INDENT total += root . data NEW_LINE DEDENT leafSum ( root . left ) NEW_LINE leafSum ( root . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . left . right = Node ( 8 ) NEW_LINE total = 0 NEW_LINE leafSum ( root ) NEW_LINE print ( total ) NEW_LINE DEDENT
Inorder Tree Traversal without recursion and without stack ! | A binary tree node ; Generator function for iterative inorder tree traversal ; Find the inorder predecessor of current ; Make current as right child of its inorder predecessor ; Revert the changes made in the ' if ' part to restore the original tree . i . e . , fix the right child of predecessor ; Constructed binary tree is 1 / \ 2 3 / \ 4 5
class Node : NEW_LINE INDENT def __init__ ( self , data , left = None , right = None ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def morris_traversal ( root ) : NEW_LINE INDENT current = root NEW_LINE while current is not None : NEW_LINE INDENT if current . left is None : NEW_LINE INDENT yield current . data NEW_LINE current = current . right NEW_LINE DEDENT else : NEW_LINE INDENT pre = current . left NEW_LINE while pre . right is not None and pre . right is not current : NEW_LINE INDENT pre = pre . right NEW_LINE DEDENT if pre . right is None : NEW_LINE INDENT pre . right = current NEW_LINE current = current . left NEW_LINE DEDENT else : NEW_LINE INDENT pre . right = None NEW_LINE yield current . data NEW_LINE current = current . right NEW_LINE DEDENT DEDENT DEDENT DEDENT root = Node ( 1 , right = Node ( 3 ) , left = Node ( 2 , left = Node ( 4 ) , right = Node ( 5 ) ) ) NEW_LINE for v in morris_traversal ( root ) : NEW_LINE INDENT print ( v , end = ' ▁ ' ) NEW_LINE DEDENT
Sum of leaf nodes at minimum level | Python3 implementation to find the sum of leaf node at minimum level ; Structure of a node in binary tree ; function to find the sum of leaf nodes at minimum level ; if tree is empty ; if there is only root node ; Queue used for level order traversal ; push rioot node in the queue ; count no . of nodes present at current level ; traverse current level nodes ; get front element from ' q ' ; if node is leaf node ; accumulate data to ' sum ' ; set flag = 1 to signify that we have encountered the minimum level ; if top 's left or right child exist  push them to Queue ; return the sum ; Driver code ; binary tree creation
from collections import deque 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 sumOfLeafNodesAtLeafLevel ( root ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return 0 NEW_LINE DEDENT if not root . left and not root . right : NEW_LINE INDENT return root . data NEW_LINE DEDENT Queue = deque ( ) NEW_LINE sum = f = 0 NEW_LINE Queue . append ( root ) NEW_LINE while not f : NEW_LINE INDENT nc = len ( Queue ) NEW_LINE while nc : NEW_LINE INDENT top = Queue . popleft ( ) NEW_LINE if not top . left and not top . right : NEW_LINE INDENT sum += top . data NEW_LINE f = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if top . left : NEW_LINE INDENT Queue . append ( top . left ) NEW_LINE DEDENT if top . right : NEW_LINE INDENT Queue . append ( top . right ) NEW_LINE DEDENT DEDENT nc -= 1 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . left . right . left = Node ( 8 ) NEW_LINE root . right . left . right = Node ( 9 ) NEW_LINE print ( " Sum ▁ = ▁ " , sumOfLeafNodesAtLeafLevel ( root ) ) NEW_LINE DEDENT
Root to leaf path sum equal to a given number | A binary tree node ; 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 . s is the sum ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2
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 hasPathSum ( node , s ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return ( s == 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = 0 NEW_LINE subSum = s - node . data NEW_LINE if ( subSum == 0 and node . left == None and node . right == None ) : NEW_LINE INDENT return True NEW_LINE DEDENT if node . left is not None : NEW_LINE INDENT ans = ans or hasPathSum ( node . left , subSum ) NEW_LINE DEDENT if node . right is not None : NEW_LINE INDENT ans = ans or hasPathSum ( node . right , subSum ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT s = 21 NEW_LINE root = Node ( 10 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 2 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . left . left = Node ( 3 ) NEW_LINE root . right . left = Node ( 2 ) NEW_LINE if hasPathSum ( root , s ) : NEW_LINE INDENT print " There ▁ is ▁ a ▁ root - to - leaf ▁ path ▁ with ▁ sum ▁ % d " % ( s ) NEW_LINE DEDENT else : NEW_LINE INDENT print " There ▁ is ▁ no ▁ root - to - leaf ▁ path ▁ with ▁ sum ▁ % d " % ( s ) NEW_LINE DEDENT
Sum of all the numbers that are formed from root to leaf paths | Python program to find sum of all paths from root to leaves A Binary tree node ; Returs sums of all root to leaf paths . The first parameter is root of current subtree , the second paramete "r 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 treePathSumUtil ( ) ; Pass the initial value as 0 as ther is nothing above root ; Driver function to test above function
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 treePathsSumUtil ( root , val ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT val = ( val * 10 + root . data ) NEW_LINE if root . left is None and root . right is None : NEW_LINE INDENT return val NEW_LINE DEDENT return ( treePathsSumUtil ( root . left , val ) + treePathsSumUtil ( root . right , val ) ) NEW_LINE DEDENT def treePathsSum ( root ) : NEW_LINE INDENT return treePathsSumUtil ( root , 0 ) NEW_LINE DEDENT root = Node ( 6 ) NEW_LINE root . left = Node ( 3 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . left = Node ( 2 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . right = Node ( 4 ) NEW_LINE root . left . right . left = Node ( 7 ) NEW_LINE root . left . right . right = Node ( 4 ) NEW_LINE print " Sum ▁ of ▁ all ▁ paths ▁ is " , treePathsSum ( root ) NEW_LINE
Merge Two Binary Trees by doing Node Sum ( Recursive and Iterative ) | Helper class that allocates a new node with the given data and None left and right pointers . ; Given a binary tree , prits 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 construct the first Binary Tree 1 / \ 2 3 / \ \ 4 5 6 ; Let us construct the second Binary Tree 4 / \ 1 7 / / \ 3 2 6
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 inorder ( node ) : NEW_LINE INDENT if ( not node ) : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE inorder ( node . right ) NEW_LINE DEDENT def MergeTrees ( t1 , t2 ) : NEW_LINE INDENT if ( not t1 ) : NEW_LINE INDENT return t2 NEW_LINE DEDENT if ( not t2 ) : NEW_LINE INDENT return t1 NEW_LINE DEDENT t1 . data += t2 . data NEW_LINE t1 . left = MergeTrees ( t1 . left , t2 . left ) NEW_LINE t1 . right = MergeTrees ( t1 . right , t2 . right ) NEW_LINE return t1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = newNode ( 1 ) NEW_LINE root1 . left = newNode ( 2 ) NEW_LINE root1 . right = newNode ( 3 ) NEW_LINE root1 . left . left = newNode ( 4 ) NEW_LINE root1 . left . right = newNode ( 5 ) NEW_LINE root1 . right . right = newNode ( 6 ) NEW_LINE root2 = newNode ( 4 ) NEW_LINE root2 . left = newNode ( 1 ) NEW_LINE root2 . right = newNode ( 7 ) NEW_LINE root2 . left . left = newNode ( 3 ) NEW_LINE root2 . right . left = newNode ( 2 ) NEW_LINE root2 . right . right = newNode ( 6 ) NEW_LINE root3 = MergeTrees ( root1 , root2 ) NEW_LINE print ( " The ▁ Merged ▁ Binary ▁ Tree ▁ is : " ) NEW_LINE inorder ( root3 ) NEW_LINE DEDENT
Merge Two Binary Trees by doing Node Sum ( Recursive and Iterative ) | 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 None 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 construct the first Binary Tree 1 / \ 2 3 / \ \ 4 5 6 ; Let us construct the second Binary Tree 4 / \ 1 7 / / \ 3 2 6
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 class snode : NEW_LINE INDENT def __init__ ( self , l , r ) : NEW_LINE INDENT self . l = l NEW_LINE self . r = r NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT new_node = Node ( data ) NEW_LINE return new_node NEW_LINE DEDENT def inorder ( node ) : NEW_LINE INDENT if ( not node ) : NEW_LINE INDENT return ; NEW_LINE DEDENT inorder ( node . left ) ; NEW_LINE print ( node . data , end = ' ▁ ' ) ; NEW_LINE inorder ( node . right ) ; NEW_LINE DEDENT def MergeTrees ( t1 , t2 ) : NEW_LINE INDENT if ( not t1 ) : NEW_LINE INDENT return t2 ; NEW_LINE DEDENT if ( not t2 ) : NEW_LINE INDENT return t1 ; NEW_LINE DEDENT s = [ ] NEW_LINE temp = snode ( t1 , t2 ) NEW_LINE s . append ( temp ) ; NEW_LINE n = None NEW_LINE while ( len ( s ) != 0 ) : NEW_LINE INDENT n = s [ - 1 ] NEW_LINE s . pop ( ) ; NEW_LINE if ( n . l == None or n . r == None ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT n . l . data += n . r . data ; NEW_LINE if ( n . l . left == None ) : NEW_LINE INDENT n . l . left = n . r . left ; NEW_LINE DEDENT else : NEW_LINE INDENT t = snode ( n . l . left , n . r . left ) NEW_LINE s . append ( t ) ; NEW_LINE DEDENT if ( n . l . right == None ) : NEW_LINE INDENT n . l . right = n . r . right ; NEW_LINE DEDENT else : NEW_LINE INDENT t = snode ( n . l . right , n . r . right ) NEW_LINE s . append ( t ) ; NEW_LINE DEDENT DEDENT return t1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = newNode ( 1 ) ; NEW_LINE root1 . left = newNode ( 2 ) ; NEW_LINE root1 . right = newNode ( 3 ) ; NEW_LINE root1 . left . left = newNode ( 4 ) ; NEW_LINE root1 . left . right = newNode ( 5 ) ; NEW_LINE root1 . right . right = newNode ( 6 ) ; NEW_LINE root2 = newNode ( 4 ) ; NEW_LINE root2 . left = newNode ( 1 ) ; NEW_LINE root2 . right = newNode ( 7 ) ; NEW_LINE root2 . left . left = newNode ( 3 ) ; NEW_LINE root2 . right . left = newNode ( 2 ) ; NEW_LINE root2 . right . right = newNode ( 6 ) ; NEW_LINE root3 = MergeTrees ( root1 , root2 ) ; NEW_LINE print ( " The ▁ Merged ▁ Binary ▁ Tree ▁ is : " ) ; NEW_LINE inorder ( root3 ) ; NEW_LINE DEDENT
Find root of the tree where children id sum for every node is given | 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
def findRoot ( arr , n ) : NEW_LINE INDENT root = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT root += ( arr [ i ] [ 0 ] - arr [ i ] [ 1 ] ) NEW_LINE DEDENT return root NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 5 ] , [ 2 , 0 ] , [ 3 , 0 ] , [ 4 , 0 ] , [ 5 , 5 ] , [ 6 , 5 ] ] NEW_LINE n = len ( arr ) NEW_LINE print ( findRoot ( arr , n ) ) NEW_LINE DEDENT
Print Postorder traversal from given Inorder and Preorder 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
def search ( arr , x , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == x ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def printPostOrder ( In , pre , n ) : NEW_LINE INDENT root = search ( In , pre [ 0 ] , n ) NEW_LINE if ( root != 0 ) : NEW_LINE INDENT printPostOrder ( In , pre [ 1 : n ] , root ) NEW_LINE DEDENT if ( root != n - 1 ) : NEW_LINE INDENT printPostOrder ( In [ root + 1 : n ] , pre [ root + 1 : n ] , n - root - 1 ) NEW_LINE DEDENT print ( pre [ 0 ] , end = " ▁ " ) NEW_LINE DEDENT In = [ 4 , 2 , 5 , 1 , 3 , 6 ] NEW_LINE pre = [ 1 , 2 , 4 , 5 , 3 , 6 ] NEW_LINE n = len ( In ) NEW_LINE print ( " Postorder ▁ traversal ▁ " ) NEW_LINE printPostOrder ( In , pre , n ) NEW_LINE
Lowest Common Ancestor in a Binary Tree | Set 1 | A binary tree node ; 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 program to test above function Let us create a binary tree given in the above example
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def findLCA ( root , n1 , n2 ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return None NEW_LINE DEDENT if root . key == n1 or root . key == n2 : NEW_LINE INDENT return root NEW_LINE DEDENT left_lca = findLCA ( root . left , n1 , n2 ) NEW_LINE right_lca = findLCA ( root . right , n1 , n2 ) NEW_LINE if left_lca and right_lca : NEW_LINE INDENT return root NEW_LINE DEDENT return left_lca if left_lca is not None else right_lca NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE print " LCA ( 4,5 ) ▁ = ▁ " , findLCA ( root , 4 , 5 ) . key NEW_LINE print " LCA ( 4,6 ) ▁ = ▁ " , findLCA ( root , 4 , 6 ) . key NEW_LINE print " LCA ( 3,4 ) ▁ = ▁ " , findLCA ( root , 3 , 4 ) . key NEW_LINE print " LCA ( 2,4 ) ▁ = ▁ " , findLCA ( root , 2 , 4 ) . key NEW_LINE