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

Spread the post

Print all leaf nodes of given binary tree

Display all leaf nodes of given binary tree, without using recursion.

For example Given Binary tree are contain following nodes.

13762485910

Result

View leaf nodes.

13762485910

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

Try it Yourself

Program for print out all leaf node of a binary tree in c. using queue.


Output

View process

Stack Areamainhead(pointer)root(pointer)tail(pointer)display_leaftemp(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) struct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer) struct Queuelink (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: