Cousins of a Given Node in a Binary Tree

Given a binary tree with 'N' nodes and a specific node in this tree, you need to determine and return a sorted list of the values of the node's cousins. The cousins should be sorted in non-decreasing order based on their values.

Definition:

In a binary tree, two nodes are considered cousins if they are at the same depth or level but have different parent nodes.

Note:

    This tree setup has unique data values; hence no two nodes will have identical values.

Input:

The input starts with a single integer T, representing the number of test cases.
For each test case:
- The first line consists of node values of the binary tree provided in level order format (use -1 for NULL nodes).
- The second line provides the specific node value for which cousins need to be identified.

Output:

- For each test case, return the sorted list of cousin node values.
- If the node has no cousins, return -1.

Example:

Consider a binary tree:

Input:

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

Output:

[5, 6]

Explanation: For the node with value 7, the cousins at the same level are 5 and 6.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 3000
  • 1 <= nodeVal <= 109
  • Time Limit: 1 second
AnswerBot
4mo

Given a binary tree and a specific node, return a sorted list of the values of the node's cousins.

  • Traverse the binary tree to find the parent of the given node and its depth.

  • Traverse the tree again to...read more

Help your peers!
Select
Add answer anonymously...

Samsung Frontend Developer Intern interview questions & answers

A Frontend Developer Intern was asked Q. Rat in a Maze Problem Statement You need to determine all possible paths for a r...read more
A Frontend Developer Intern was asked Q. What is a process in an operating system?
A Frontend Developer Intern was asked Q. Cousins of a Given Node in a Binary Tree Given a binary tree with 'N' nodes and ...read more

Popular interview questions of Frontend Developer Intern

A Frontend Developer Intern was asked Q1. Rat in a Maze Problem Statement You need to determine all possible paths for a r...read more
A Frontend Developer Intern was asked Q2. What is a process in an operating system?
A Frontend Developer Intern was asked Q3. Cousins of a Given Node in a Binary Tree Given a binary tree with 'N' nodes and ...read more
Samsung Frontend Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits