In case of array representation of this binary tree where we can traverse through all the leaf nodes and move to their parents, this problem becomes quite easy. for the example stated consider its array representation arr[]={1,2,3,4,5,6,7} and take another array marked[7]={false} indicating whether the current node has been printed
Now for every non leaf node, index i in (n/2) to (n-1) print the leaf node and mark its marked[i]=true change j= floor(i-1)/2 while marked[j] = false: print arr[j] marked[j]=true change j=floor(j-1)/2 a C++ implementation for the above:- int main(int argc, char **argv) { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }, N = 7; bool marked[7]; for (int i = 0; i < N; i++) marked[i] = false; for (int i = N / 2; i < N; i++) { printf("[%d]", arr[i]); marked[i] = true; for (int j = (i - 1) / 2; j>=0 && !marked[j]; j = (j - 1) / 2) { printf("[%d]", arr[j]); marked[j] = true; } } } Anurag Sharma On Wed, Jun 9, 2010 at 9:31 PM, sharad kumar <sharad20073...@gmail.com>wrote: > >>> >>> >>> 1 >>> / \ >>> 2 3 >>> / \ / \ >>> 4 5 6 7 >>> >>> >>> print >>> >>> 4 2 1 5 6 3 7 >>> >>> > a set of vertical line 4m left u draw then on each line whatever no comes > write it > >> >> >> >> -- >> Luv, >> S.Antony Vincent Pandian >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algoge...@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@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 algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@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 algoge...@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.