You can use two stacks(A, B) when traversal.

Use A as the stack when traveling the odd levels.
B as the even levels.

A.push(1);
A.pop(); //1

//push the son of the node 1, from left to right;
B.push(2);
B.push(3);

B.pop(); // 3

//push the son of the node 3, from right to left
A.push(7);
A.push(6);

B.pop(); // 2
A.push(5);
A.push(4);

A.pop(); //4
A.pop(); //5
A.pop(); //6
A.pop(); //7

Just like this.

On Sun, Aug 28, 2011 at 7:03 AM, Navneet Gupta <navneetn...@gmail.com>wrote:

> Print tree in zig zag manner
>
>     1
>    /   \
>  2        3
>  / \       / \
> 4   5    6   7
>
> O/P: 1 3 2 4 5 6 7
>
>
> --
> Regards,
> Navneet
>
> --
> 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.
>
>


-- 
from Yuchen Liao via Gmail

-- 
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.

Reply via email to