Spread the post

Level order traversal in spiral form

Level order traversal of a binary tree in spiral form without using recursion.

For example Given Binary tree are contain following nodes.

12594836107

Result

Level Order Spiral Form : 1 2 3 7 6 5 4 8 9 10 .

Hint:User two stack to solve this problem.

View pointers and nodes of tree.

Stack Areamainroot(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer) right (pointer) struct Treedata (int)= 4left (pointer) right (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULL

Try it Yourself

C program for Spiral traversal of binary tree using stack.


Output

View process

Stack Areamainroot(pointer)spiral_formauxiliary(pointer)temp(pointer)top1(pointer) NULLtop2(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer) right (pointer) struct Treedata (int)= 4left (pointer) right (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Stacklink (pointer) next (pointer)= NULLstruct Stacklink (pointer) next (pointer)

Try it Yourself

Spread the post

Recommended Posts: