Spread the post

Print all root to leaf paths in a binary tree

Print all paths in binary tree from root node to all leaf nodes.

For example Given Binary tree are contain following nodes.

12475836910

Result

Hint: Using Inorder tree traversal methods. if find leaf node then print data in reverse order.

View pointers and nodes of tree.

Stack Areamainroot(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 all possible paths from root node to leaf nodes in a binary tree.


Output

View process

Stack Areamainroot(pointer)top(pointer)path_printstore(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) 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 Stackvisit (int)= 2link (pointer) next (pointer)= NULLstruct Stackvisit (int)= 2link (pointer) next (pointer) struct Stackvisit (int)= 1link (pointer) next (pointer) struct Stackvisit (int)= 2link (pointer) next (pointer) struct Stackvisit (int)= 0link (pointer) next (pointer)

Try it Yourself

Spread the post

Recommended Posts: