recategorized by
827 views
0 votes
0 votes
what is the time complexity to construct binary search tree when preorder and postorder of tree given?

answer given is O(n)

but i think it will be nlogn...becz from given postorder or preorder we need to find in order by sortingin nlogn tum so I think overalltime complexity will be nlogn ..???
recategorized by

1 Answer

1 votes
1 votes

let me answer this question if preorder or postorder traversal of binary search tree is given  then we can find inorder traversal by sorting the elements in ascending order using efficient sorting algorithm in O(nlogn) time and with the help of inorder and postorder or inorder and preorder we can construct BST in O(nlogn) time ,

so total time complexity =O(nlogn)+O(nlogn)=O(nlogn)

But why to find inorder traversal to construct BST if preorder or postorder is given?

we can construct BST with  preorder or postorder only using efficient algorithm like stack method in O(n) time.

https://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/

https://www.geeksforgeeks.org/construct-a-binary-search-tree-from-given-postorder/

edited by

Related questions

1 votes
1 votes
1 answer
1
Kaluti asked Mar 31, 2018
534 views
give an o(n) algorithm to check whether the given binary tree is bst or not