Spread the post

Print all nodes that don’t have sibling

Check the following condition and find sibling of binary tree.

1) parent node left subtree are exist but no element on right subtree.

2) parent node right subtree are exist but no element on left subtree.

Note that if only one node of tree that means no sibling.

For example given Binary tree are contain following nodes.

13762485910

result: 8 9 10

Function: Print nodes that have no sibling of given binary tree.

View pointers and nodes of Binary 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 nodes that have no sibling of given binary tree. without recursion.


Output

View process

Stack Areamainhead(pointer)root(pointer)tail(pointer)print_siblingstemp(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: