# Right sibling of each node in a tree given as array of edges

Given a tree, with *N* nodes and *E* edges (every edge is denoted by two integers, *X, Y* stating that X is the parent of Y), the task is to print all the nodes with their right siblings in separate lines.

If there is no right sibling for a particular node then print *-1* instead.

**Examples:**

In the image shown above, nodes 3, 5, 6, 7 are the right siblings of nodes 2, 4, 5, 6 respectively and the nodes 1, 3 and 7 have no right siblings.

Input:N = 7, E = 6

edges[][] = {{1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {3, 7}}

Output:1 -1

2 3

3 -1

4 5

5 6

6 7

7 -1

Input:N = 5, E = 4

edges[][] = {{7, 8}, {7, 10}, {7, 15}, {10, 3}}

Output:7 -1

8 10

10 15

15 -1

3 -1

**Approach:** The main idea is to use a Breadth First Traversal.

- Initially, the root node and ‘-1’ value will be pushed into the queue. After every node from a particular level in the tree has been pushed to the queue, ‘-1’ has to be pushed to make sure that the last node in the level has no right sibling.
- After popping each node from the queue, the node at the front of the queue will always be the right sibling of the popped node.
- If the popped node is valued ‘-1’, it means the current level has been visited and if the queue is not empty it means that previous nodes of this level have at least one child which hasn’t been visited.
- Repeat the above steps while the queue is non-empty.

Below is the implementation of the above approach:

`// C++ program to print right siblings ` `// of all the nodes in a tree ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `PrintSiblings(` `int` `root, ` `int` `N, ` `int` `E, vector<` `int` `> adj[]) ` `{ ` ` ` `// boolean array to mark the visited nodes ` ` ` `vector<` `bool` `> vis(N+1, ` `false` `); ` ` ` ` ` `// queue data structure to implement bfs ` ` ` `queue<` `int` `> q; ` ` ` `q.push(root); ` ` ` `q.push(-1); ` ` ` `vis[root] = 1; ` ` ` `while` `(!q.empty()) { ` ` ` `int` `node = q.front(); ` ` ` `q.pop(); ` ` ` `if` `(node == -1) { ` ` ` ` ` `// if queue is empty then ` ` ` `// the popped node is the last node ` ` ` `// no need to push -1. ` ` ` `if` `(!q.empty()) ` ` ` `q.push(-1); ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// node and its right sibling ` ` ` `cout << node << ` `" "` `<< q.front() << ` `"\n"` `; ` ` ` `for` `(` `auto` `s : adj[node]) { ` ` ` `if` `(!vis[s]) { ` ` ` `vis[s] = 1; ` ` ` `q.push(s); ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// nodes and edges ` ` ` `int` `N = 7, E = 6; ` ` ` `vector<` `int` `> adj[N+1]; ` ` ` ` ` `// The tree is represented in the form of ` ` ` `// an adjacency list as there can be ` ` ` `// multiple children of a node ` ` ` `adj[1].push_back(2); ` ` ` `adj[1].push_back(3); ` ` ` `adj[2].push_back(4); ` ` ` `adj[2].push_back(5); ` ` ` `adj[3].push_back(6); ` ` ` `adj[3].push_back(7); ` ` ` `int` `root = 1; ` ` ` `PrintSiblings(root, N, E, adj); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

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

## Recommended Posts:

- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Left-Child Right-Sibling Representation of Tree
- Find right sibling of a binary tree with parent pointers
- Creating a tree with Left-Child Right-Sibling Representation
- Possible edges of a tree for given diameter, height and vertices
- Level of Each node in a Tree from source node (using BFS)
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Number of edges in mirror image of Complete binary tree
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal
- XOR of all the nodes in the sub-tree of the given node
- Minimum cost path from source node to destination node via an intermediate node
- Print all nodes that don't have sibling
- Search a node in Binary Tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.