Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Print level order traversal of a tree in reverse order

Raversal level order traversal of binary tree without recursion.

For example Given Binary tree are contain following nodes.

12475836910

Reverse Level Order Traversal : 10 9 8 7 6 5 4 3 2 1.

hint: Solve this problem using the one queue and one stack.

View pointers and nodes of tree.

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

Try it Yourself

C program for print level order traversal of a binary tree in reverse way. Time complexity O(n).


Output

View process

Stack Areamainhead(pointer) NULLroot(pointer)top(pointer)reverse_orderauxiliary(pointer)head(pointer)tail(pointer)temp(pointer)top(pointer)popremove(pointer)top(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer) right (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer) struct Treedata (int)= 6left (pointer) right (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Stacklink (pointer) next (pointer)= NULLstruct Stacklink (pointer) next (pointer) struct Stacklink (pointer) next (pointer)

Try it Yourself

Spread the post

Recommended Posts: