Right Sibling in Binary Tree
Submissions: 1285   Accuracy:

27.42%

  Difficulty: Medium   Marks: 4
     

Given a binary tree, your task is to complete the function findRightSibling(), that should return the right sibling to a given node if it doesn’t exist return null.

Structure of the node of the binary tree is as
struct Node
{
    int data;
    Node* left;
    Node* right;
    Node* parent;
};

Examples:

             1
            / \
           2   3
          /  \  \
         4    6  5
        /      \  \
       7        9  8
       /         \
      10         12
Input : Given above tree with parent pointer and node 10
Output : 12

Input:
The function takes a single argument as input, the reference pointer to the node whose right sibling is to be found in the provided binary tree.
There will be T, test cases and for each test case, the function will be called separately.

Output:
For each test case return the pointer to the right sibling of a given node.

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:

2
10
1 2 L 1 3 R 2 4 L 2 6 R 4 7 L 7 10 L 6 9 R 9 12 R 3 5 R 5 8 R
7
2
1 2 L 1 3 R
2
Output:
9
3

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: harshitsidhwa


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.