Preorder to Postorder Python

PROGRAM TO FIND POSTORDER TRAVERSAL OF BST FROM PREORDER TRAVERSAL



def getPostOrderBST(pre, N):
    pivotPoint = 0
 
    # Run loop from 1 to length of pre
    for i in range(1, N):
        if (pre[0] <= pre[i]):
            pivotPoint= i
            break
 
    # Prfrom pivot length -1 to zero
    for i in range(pivotPoint - 1, 0, -1):
        print(pre[i], end = " ")
 
    # Prfrom end to pivot length
    for i in range(N - 1, pivotPoint - 1, -1):
        print(pre[i], end = " ")
    print(pre[0])
 
# Driver Code
if __name__ == '__main__':
    pre = [40, 30, 32, 35,80, 90, 100, 120]
    N = 8
 
    getPostOrderBST(pre, N)


OUTPUT:
35 32 30 120 100 90 80 40

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java