Given a binary tree, Print All the Nodes that don’t have Siblings.

Objective: Given a binary tree, Print All the Nodes that don’t have siblings.

Note: sibling node is the node which has the same parent, so you need to print the nodes who is a single child of his parent.

Input: A binary tree.

Example:

Print All the Nodes that don't have siblings.

Print All the Nodes that don’t have siblings.

Approach:

  • Do the inorder traversal.
  • check if node has only child, if yes then print that child.

Complete Code:

Output:

Nodes with No Siblings: 6 7 8 9

 

 

__________________________________________________
Top Companies Interview Questions..-

GoogleMicrosoftAmazonFacebookmore..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

1 Response

  1. Kamal Chaya says:

    Why do we have to do in order traversal? won’t pre order work?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: