Made some changes.

flattenTree(TreeNode node,TreeNode previous) {
   if (node is leaf) {
      previous = node
      return;
    }
    flattenTree(node->left,previous);
    if (previous != null) {
        previous->next = node;
        previous=node;
    }

    flattenTree(node->right,previous);
}//End-flattenTree

On Sat, Aug 28, 2010 at 9:03 PM, Giri <giri.pe...@gmail.com> wrote:

> @Chonku:
> yours is wrong. consider the given ex,.
>        50
>      /     \
>   25      60
>  /     \     /  \
> 5    30  55  75
>
> 5 will become head. 5->next=25. 25->next=30. then 25 will be returned
> up.
> so 25->next=50. which is wrong
>
> On Aug 26, 11:36 pm, Chonku <cho...@gmail.com> wrote:
> > At first, store the pointer to the first node in inorder traversal (in
> this
> > case 5) because its going to be the head of the list.
> > Then use the following logic.
> >
> > flattenTree(TreeNode node) {
> >     if (node is leaf node)
> >        return node;
> >
> >     if (node.left exists) then
> >         flattenTree(node.left)->next = node;
> >
> >      if (node.right exists) then
> >         node->next = flattenTree(node.right);
> >
> >       return node;
> >
> > }
> >
> > On Thu, Aug 26, 2010 at 11:07 PM, Yan Wang <wangyanadam1...@gmail.com
> >wrote:
> >
> >
> >
> > > I can only figure out the inorder traversal...
> >
> > > On Thu, Aug 26, 2010 at 9:59 AM, krazee koder <aravindhr...@gmail.com>
> > > wrote:
> > > > Give all possible methods to flatten a binary tree to a linked list.
> >
> > > > for eg.
> >
> > > >       50
> > > >     /     \
> > > >  25      60
> > > > /     \     /  \
> > > > 5    30  55  75
> >
> > > > should be flattened to  5->25->30->50->55->60->75
> >
> > > > PS: note that the tree should be converted to the LL and no separate
> > > > LL should be formed.
> >
> > > > --
> > > > 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>
> <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>
> <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.

Reply via email to