human
What is a code to leaf nodes from Preorder of a Binary Search Tree and the other steps required given the following:
Leaf nodes from Preorder of a Binary Search Tree | Binary Search.
Poto the index in preorder ..
Function to prLeaf Nodes by doing preorder traversal of tree using preorder and inorder arrays ..
If l == r , therefore no right or left subtree . So , it must be leaf Node , print it ..
If array is out of bound , return ..
Finding the index of preorder element in inorder array using binary search ..
Incrementing the index ..
Finding on the left subtree ..
Finding on the right subtree ..
Finds leaf nodes from given preorder traversal ..
To store inorder traversal.
Copy the preorder into another array ..
Finding the inorder by sorting the array ..
Print the Leaf Nodes ..
Driver Code"
Solution in Python:
assistant
#Here is code to leaf nodes from Preorder of a Binary Search Tree and the other steps required.
 def binarySearch ( inorder , l , r , d ) :
	mid = ( l + r ) >> 1
	if ( inorder [ mid ] == d ) :
		return mid
	elif ( inorder [ mid ] > d ) :
		return binarySearch ( inorder , l , mid - 1 , d )
	else :
		return binarySearch ( inorder , mid + 1 , r , d )
	ind = [ 0 ]
def leafNodesRec ( preorder , inorder , l , r , ind , n ) :
	if ( l == r ) :
		print ( inorder [ l ] , end = " ▁ " )
		ind [ 0 ] = ind [ 0 ] + 1
		return
	if ( l < 0 or l > r or r >= n ) :
		return
	loc = binarySearch ( inorder , l , r , preorder [ ind [ 0 ] ] )
	ind [ 0 ] = ind [ 0 ] + 1
	leafNodesRec ( preorder , inorder , l , loc - 1 , ind , n )
	leafNodesRec ( preorder , inorder , loc + 1 , r , ind , n )
def leafNodes ( preorder , n ) :
	inorder = [ 0 ] * n
	for i in range ( n ) :
		inorder [ i ] = preorder [ i ]
	inorder . sort ( )
	ind = [ 0 ]
	leafNodesRec ( preorder , inorder , 0 , n - 1 , ind , n )
preorder = [ 890 , 325 , 290 , 530 , 965 ]
n = len ( preorder )
leafNodes ( preorder , n )