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

Spread the post

Reverse level order traversal

Print data in reverse order. write an efficient algorithm to solve this problem.

Example

Suppose following nodes are inserted on binary tree.

13682574

Result

Hint: solve this problem by using a queue and stack.

View nodes and pointers.

Stack Areamainroot(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)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULL

Try it Yourself

Program for Reverse level order traversal of Binary tree. iterative solution using queue and stack.


Output

Stack Areamainroot(pointer)reverse_levelauxiliary(pointer) NULLhead(pointer) NULLhelper(pointer) NULLtail(pointer)temp(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)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Stacklink (pointer) next (pointer)= NULLstruct Stacklink (pointer) next (pointer) struct Stacklink (pointer) next (pointer) struct Stacklink (pointer) next (pointer) struct Stacklink (pointer) next (pointer) struct Stacklink (pointer) next (pointer) struct Stacklink (pointer) next (pointer)

Visualize process Try it Yourself

Spread the post

Recommended Posts: