Cousin Nodes in a Binary Tree

Determine if two nodes in a binary tree are cousins. Nodes are considered cousins if they are at the same level and have different parents.

Explanation:

In a binary tree, each node has a value, and each tree node has at most two children. Nodes are on the same level if their distance from the root is the same. To be cousins, nodes must also have different parents.

Input:

The first line contains an integer 'T' representing the number of test cases.
For each test case, the first line contains tree elements in level-order, separated by spaces.
The second line contains two space-separated integers representing node values to be checked.

Output:

For each test case, return "YES" if the nodes are cousins, "NO" otherwise.

Example:

Input:
2
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
4 7
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
3 5
Output:
YES
NO
Explanation:

In the first test case, nodes 4 and 7 are cousins as they are on the same level and have different parents. In the second test case, nodes 3 and 5 are not cousins as they do not meet the criteria.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 1000
  • -10^6 ≤ data ≤ 10^6
  • All nodes have distinct values.
  • -1 represents a null node.
  • Time Limit: 1 sec

Note:

The input for each test case is a single line of integers in level order, where -1 represents a null position.

AnswerBot
4mo

Determine if two nodes in a binary tree are cousins based on level and parent nodes.

  • Traverse the binary tree to find the levels and parents of the given nodes.

  • Check if the nodes are at the same level ...read more

Help your peers!
Select
Add answer anonymously...

Paytm Full Stack Developer interview questions & answers

A Full Stack Developer was asked Q. Sort 0 1 2 Problem Statement Given an integer array arr of size 'N' containing o...read more
A Full Stack Developer was asked Q. What do you know about Paytm?
A Full Stack Developer was asked Q. Cousin Nodes in a Binary Tree Determine if two nodes in a binary tree are cousin...read more

Popular interview questions of Full Stack Developer

A Full Stack Developer was asked Q1. Sort 0 1 2 Problem Statement Given an integer array arr of size 'N' containing o...read more
A Full Stack Developer was asked Q2. What do you know about Paytm?
A Full Stack Developer was asked Q3. Cousin Nodes in a Binary Tree Determine if two nodes in a binary tree are cousin...read more
Paytm Full Stack Developer 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