the complexity is N^2 for each level you are traversing all the nodes below that level. for nth level , you have to traverse all n-1 nodes. so O(N^2) better to use queue , we can traverse level order in O(n). On Sat, Nov 19, 2011 at 4:58 PM, shady <sinv...@gmail.com> wrote:
> this doesn't seem like level order printing, because you are simply > printing the tree starting with the children as the root node. > > > On Sat, Nov 19, 2011 at 12:57 PM, Ankuj Gupta <ankuj2...@gmail.com> wrote: > >> What is the time complexity of this code for Level Order Traversal. >> >> void printLevel(BinaryTree *p, int level) { >> if (!p) return; >> if (level == 1) { >> cout << p->data << " "; >> } else { >> printLevel(p->left, level-1); >> printLevel(p->right, level-1); >> } >> } >> >> void printLevelOrder(BinaryTree *root) { >> int height = maxHeight(root); >> for (int level = 1; level <= height; level++) { >> printLevel(root, level); >> cout << endl; >> } >> } >> >> My guess is NlogN if tree is balanced if not it will be N^2. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- * Regards* *"The Coder"* *"Life is a Game. The more u play, the more u win, the more u win , the more successfully u play"* -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.