void printLevelOrder(BinaryTree *root) { node *temp; enqueue(root); while((temp=dequeue()) != NULL) { printf("%d ",temp->data);
if (temp->left) { enqueue(temp->left); } if ( temp->right) { enqueue(temp->right); } } } i guess this approach is much better. :) On Sat, Nov 19, 2011 at 9:06 PM, atul anand <atul.87fri...@gmail.com> wrote: > @shady : i guess code is fine....it will print the node only when level=1 > , and level is in loop so for level = 1 it will print root > when level=2 then it will print all nodes at level 2 and so on... > > i guess we can optimize this code...... > > > On Sat, Nov 19, 2011 at 3:28 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. >> > > -- 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.