Problem Statement: Sibling Nodes

You are provided with a Binary Tree consisting of 'N' nodes, where each node holds an integer value. Your objective is to identify and list all nodes that do not possess a sibling.

Note:
1. A node ‘U’ is defined as a sibling of node ‘V’ if both nodes share the same parent.
2. The root node is considered a sibling node.

Input:

The first input line contains an integer 'T', indicating the number of test cases or queries.

Each test case comprises a single line of space-separated integers representing the Binary Tree in level order. Each value corresponds to a node, and '-1' represents a null node.

Output:

For each test case, output a single line containing the values of all nodes without siblings, sorted in ascending order.
The output for each test case should appear on a new line.

Example:

Input:

1
1 3 8 5 2 7 -1 -1 -1 -1 -1

Output:

7

Constraints:

  • 1 <= T <= 5
  • 0 <= N <= 3000
  • 0 <= node data <= 109

Where 'T' is the number of test cases and 'N' is the number of nodes in the tree.

Time limit: 1 second.

Note:
You are not required to handle printing the output; this is managed for you. Implement the function to achieve the desired outputs.
AnswerBot
1d

Identify and list nodes in a Binary Tree that do not have a sibling.

  • Traverse the Binary Tree in level order to identify sibling nodes.

  • Keep track of nodes without siblings and output them in ascending ...read more

Help your peers!
Add answer anonymously...
Directi Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter