Spread the post

Print path between two nodes in a binary tree

Print path between two nodes in a binary tree. bottom-up approach without using recursion.

For example Given Binary tree are contain following nodes.

12594836107

Result

Hint: User queue to solve this problem.

View pointers and nodes of tree.

Stack Areamainhead(pointer) NULLroot(pointer)tail(pointer) NULL 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 print path between two nodes in a tree. using queue.


Output

View process

Stack Areamainhead(pointer)root(pointer)tail(pointer)print_pathauxiliary1(pointer)auxiliary2(pointer) NULLhead(pointer)node1 (int) =8node2 (int) =8temp(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 Queuelevel (int)= 0link (pointer) parent (pointer)= NULLnext (pointer) struct Queuelevel (int)= 1link (pointer) parent (pointer) next (pointer) struct Queuelevel (int)= 1link (pointer) parent (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) parent (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) parent (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) parent (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) parent (pointer) next (pointer) struct Queuelevel (int)= 3link (pointer) parent (pointer) next (pointer) struct Queuelevel (int)= 3link (pointer) parent (pointer) next (pointer) struct Queuelevel (int)= 3link (pointer) parent (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: