in DS recategorized
8,918 views
0 votes
0 votes

The runtime for traversing all the nodes of a binary search tree with $n$ nodes and printing them in an order is

  1. $O(\lg n)$
  2. $O(n \lg n)$ 
  3. $O(n)$
  4. $O(n^{2})$
in DS recategorized
8.9k views

1 comment

edited by
O(n), since we need to  visit all of them , anyway.
0
0

1 Answer

2 votes
2 votes
Best answer
Printing the elements in an order means the elements should be printed in sorted order and we know inorder traversal of BST takes gives the sorted order and takes O(n) time.The recurrence involved is :

T(n) = 2T(n/2) + c for balanced BST which on solving gives O(n) and also for skewed tree T(n) = T(n-1) + c which also on solving gives O(n) time.Hence C) is the correct option.
selected by

Related questions