Close
Topic list
Cousins of Given Node in Binary Tree
MEDIUM
10 mins
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Cousins of Given Node in Binary Tree

Contributed by
Ashwani
Medium
0/80
Avg time to solve 10 mins
Success Rate 90 %
Share

## Problem Statement

#### Given a binary tree of N nodes and a node of this tree, you need to return a list containing the values of the cousins of the given node in the given binary tree sorted by non-decreasing order of their values.

##### Note:
``````Two nodes of a binary tree are cousins if they have the same depth or level, but have different parents.

No two nodes in the given binary tree will have the same data values.
``````
##### Example :

Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1 :
``````2
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
4
1 2 3 -1 -1 -1 -1
2
``````
##### Sample Output 1 :
``````5 6
-1
``````
##### Explanation For Sample Input 1 :
``````The tree representation for test case 2 is given below.
``````

##### Sample Input 2 :
``````2
1 2 3 4 5 6 -1 7 8 -1 -1 9 10 -1 -1 -1 -1 -1 -1 -1 -1
9
1 2 -1 3 4 -1 -1 -1 -1
3
``````
##### Sample Output 2 :
``````7 8 10
-1
``````
Auto
Console