Last Updated: 11 Dec, 2020

Nodes without siblings

Easy
Asked in companies
OlaWalmartSamsung

Problem statement

You are given an arbitrary binary tree consisting of N nodes, your task is to find all the nodes of the tree that do not have a sibling.

A binary tree is a tree where every node has at most two child nodes.

Two nodes in a tree are called siblings if they are children of the same parent node i.e they have the same immediate ancestor node.

Note:
1. The root node is not considered as the node with no sibling.
2. If there are no such nodes, return -1.

For example, consider the following binary tree:

example

Here node 4 and 7 do not have any siblings.
Input Format:
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.

The only line of each test case contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 on its place.

For example, the input for the tree depicted in the below image would be:

example

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation:

Level 1:
The root node of the tree is 1

Level 2:
Left child of 1 = 2
Right child of 1 = 3

Level 3:
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4:
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5:
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null(-1). The last line contains the value of the two nodes (7 and 5) between which we have to find the distance.
Note:
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format:
For each test case, the output will be a list of nodes that do not have a sibling.

The output for each test case will be in a separate line.
Note:
You do not need to print anything; it has already been taken care of.
Constraints:
1 <= T <= 100
1 <= N <= 3000
1 <= data <= 10^6

Where ‘T’ is the number of test cases, ‘N’ is the total number of nodes in the binary tree, and “data” is the value of the binary tree node.

Time Limit: 1 second

Approaches

01 Approach

We will traverse through all the nodes of the tree and check if any node has just one child. If a node has only one child we’ll add that child to list NWS (Nodes without siblings). Consider function NODESWITHOUTSIBLINGS that take a tree node ROOT as a parameter, and do:

  1. If ROOT is NULL, return.
  2. If ROOT has both left and right child, call function NODESWITHOUTSIBLINGS for left and right child.
  3. Else, if ROOT has only left child, add ROOT.LEFT.DATA to NWS and call NODESWITHOUTSIBLINGS for ROOT.LEFT
  4. Else, if ROOT has only right child, add ROOT.RIGHT.DATA to NWS and call NODESWITHOUTSIBLINGS for ROOT.RIGHT

NWS contains the list of nodes without siblings.

02 Approach

We will iterate through all the nodes of the tree and check if any node has just one child. If a node has only one child we’ll add that child to list NWS (Nodes without siblings). Consider function NODESWITHOUTSIBLINGS that take a tree node ROOT as a parameter, and do:

  1. If ROOT is NULL, return NULL
  2. Define a Queue of BinaryTreeNode Q1 to store unprocessed nodes in it.
  3. Add ROOT to Q1.
  4. Define ArrayList of integers RESULT to store the final list of nodes without siblings.
  5. While Q1 is not EMPTY, iterate over elements of Q1 and do:
    1. Define TEMP variable and store Q1.PEEK() into it.
    2. Remove topmost element from Q1.
    3. If TEMP.LEFT is not NULL and TEMP.RIGHT is NULL:
      1. Add TEMP.LEFT.DATA to RESULT.
    4. If TEMP.RIGHT is not NULL and TEMP.LEFT is NULL:
      1. Add TEMP.RIGHT.DATA to RESULT.
    5. If TEMP.LEFT is not NULL, add TEMP.LEFT to Q1.
    6. If TEMP.RIGHT is not NULL, add TEMP.RIGHT to Q1.
    7. Return RESULT.