I've have one solution using two stacks, fs (forward stack) to print
elements in forward manner and rs to print in reverse.
Basic idea is while printing elements in forward order, put the elements in
reverse stack so that the while popping the reverse stack the last element
in row is popped first. Similary while popping reverse stack, make sure, the
first element in the next level is entered last in the stack for the next
iteration of forward printing.
if(root)
fs.push(root)
while (!fs.empty() || !rs.empty()){
while(!fs.empty()){
elem = fs.pop();
print elem.data;
if(elem->left) rs.push(elem->left);
if(elem->right) rs.push(elem->right);
}
while(!rs.empty()){
elem = rs.pop();
print elem.data;
if(elem->right) fs.push(elem->right);
if(elem->left) fs.push(elem->left);
}
}
On Mon, Nov 23, 2009 at 10:47 AM, Nayn <[email protected]> wrote:
> Honestly.. guys.. i would appreciate if we could come up with
> something concrete.
>
> On Nov 20, 5:38 pm, ganesa thandavam <[email protected]> wrote:
> > we need to use queue and stack alternately ...
> >
> > once we handle this , i think it should be straight forward to
> > code ...
> >
> > On Nov 20, 9:27 am, dinesh bansal <[email protected]> wrote:
> >
> >
> >
> > > On Wed, Nov 18, 2009 at 8:05 AM, Nayn <[email protected]>
> wrote:
> > > > Hi guys,
> > > > Recently I came across a problem. We've to display a binary tree in
> > > > spiral.
> > > > 1. We need to print the nodes in BFS manner.
> > > > 2. The nodes should be displayed in alternate direction; in one level
> > > > from left to right and in next level right to left.
> > > > Needless to mention, we need least time complex solution.
> > > > Thanks
> > > > Nayn
> >
> > > > --
> >
> > > > 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%[email protected]>
> <algogeeks%2bunsubscr...@googlegroups .com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/algogeeks?hl=.
> >
> > > > If its just about display, you can traverse the tree in BFS manner
> and
> >
> > > store the nodes in an array at their specific locations. At the end,
> display
> > > the nodes from the array.
> >
> > > Thanks,
> > > --
> > > Dinesh Bansal
> > > The Law of Win says, "Let's not do it your way or my way; let's do it
> the
> > > best way."
>
> --
>
> 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%[email protected]>
> .
> 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 [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.