Nodes without siblings

Easy
0/40
Average time to solve is 10m
2 upvotes
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.
Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1:
2
5 6 2 4 3 -1 -1 9 7 -1 -1 -1 -1 -1 -1
1 2 -1 -1 3 -1 -1
Sample Output 1:
-1
2 3
Explanation of Sample Input 1:

Sample Input 1

For the first test case, 6 and 2 are siblings, 4 and 3 are siblings and 9 and 7 are siblings. So, there are no nodes that do not have any siblings.

For the second test case, 2 and 3 do have any siblings.
Sample Input 2:
3
1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
1 -1 2 -1 3 -1 -1
1 2 -1 3 4 5 6 7 8 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output 2:
-1
2 3    
2
Hint

Try to reach out to each node and check whether it has any sibling or not.

Approaches (2)
Recursive

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.

Time Complexity

O(N), where ‘N’ is the number of nodes in the given binary tree.

Since, we’ll be traversing all, N nodes of the binary tree.

Space Complexity

O(N), Where ‘N’ is the number of nodes in the given binary tree.

Since stack space will be required to store function calls in memory.

Code Solution
(100% EXP penalty)
Nodes without siblings
Full screen
Console