Spread the post

Print rightmost nodes of a Binary Tree

Given a binary tree, print rightmost nodes of this tree. Write an efficient algorithm to solve this problem. Recommended Before start, view the post of how to print leftmost nodes of BT

Example

90108077205060

View Binary tree nodes.

Stack Areamainhead(pointer) NULLroot(pointer)tail(pointer) NULL Heap Areastruct Treedata (int)= 90left (pointer) right (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 80left (pointer) right (pointer) struct Treedata (int)= 77left (pointer) right (pointer) struct Treedata (int)= 60left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 50left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 20left (pointer)= NULLright (pointer)= NULL

View RightMost nodes

90108077205060

We are easily solve this problem using queue.

Program for print rightmost nodes of binary tree.

Output

View process.

Stack Areamainhead(pointer)root(pointer)tail(pointer)right_mosttemp(pointer) Heap Areastruct Treedata (int)= 90left (pointer) right (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 80left (pointer) right (pointer) struct Treedata (int)= 77left (pointer) right (pointer) struct Treedata (int)= 60left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 50left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 20left (pointer)= NULLright (pointer)= NULLstruct Queueheight (int)= 0link (pointer) next (pointer) struct Queueheight (int)= 1link (pointer) next (pointer) struct Queueheight (int)= 1link (pointer) next (pointer) struct Queueheight (int)= 2link (pointer) next (pointer) struct Queueheight (int)= 2link (pointer) next (pointer) struct Queueheight (int)= 3link (pointer) next (pointer) struct Queueheight (int)= 3link (pointer) next (pointer)= NULL

View steps of execution

Submit your solution in comment section.

Spread the post

Recommended Posts: