Spiral order .. means zigzag order for example

         1
    2         3
4       5  6       7

Then , you need to print it in the order
1->2-3->7->6->5->4

Two of my friends were asked this questions in the interview , so I
will list both of the approaches .

1)Use 1 stack and 1 queue .

push the elements of one level in stack 1
and the other in queue2
print both the stack and queue recursively.

algo :-
printZigZag(node * node , int level)
{
       if(node==NULL)
              return ;
       if(level==0)
       {
             level=1;
             stack1.push(node->data);
             printZigZag(node->left , level);
             printZigZag(node->right , level);
        }
        else
        {
            level=0;
            stack2.push(node->data);
            printZigZag(node->left , level)
            printZigZag(node->right , level)
        }
}
After this recursion ends , the two stacks will have the contents as
(1)                   (2)
4                      2
5                      3
6
7
1

Another approach would be to use two stacks . google it up .
and like this , now just print it recursively

On Jan 29, 10:17 pm, saurabh gupta <[email protected]> wrote:
> what do you mean by spiral order ?
>
>
>
>
>
> On Sat, Jan 29, 2011 at 8:25 PM, bittu <[email protected]> wrote:
> > Convert BT in to DLL such that DLL represents the Sprial order
> > traversal of BT.
>
> > "Inplace"
>
> > its Written Test Question & They wants Exact Working Code...Not
> > Approach..Be Clear..Try to provide best solutions
>
> > Thanks & Regards
> > Shashank " "The best way to escape from a problem is to solve it."
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<algogeeks%2Bunsubscribe@googlegroups 
> > .com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
>
> --
> Man goes to doctor. Says he's depressed. Says life seems harsh and cruel.
> Says he feels all alone in a threatening world where what lies ahead is
> vague and uncertain. Doctor says "Treatment is simple. Great clown
> Pagliacci is in town tonight. Go and see him. That should pick you up." Man
> bursts into tears. Says "But, doctor...I am Pagliacci."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to