-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cousins of a given node.cpp
54 lines (48 loc) · 1.27 KB
/
Cousins of a given node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Given a binary tree and a node, print all cousins of given node in order of their
appearance. Note that siblings should not be printed.
Example 1:
Input :
1
/ \
2 3
/ \ / \
4 5 6 7
Given node : 5
Output : 6 7
*/
vector<int> printCousins(Node* root, Node* node_to_find)
{
//code here
//we will be using dfs / level order traversal
vector<int> res;
queue<Node*> q;
q.push(root);
bool check = false;
while(!q.empty())
{
int sz = q.size();
while(sz--)
{
Node* temp = q.front();
q.pop();
if(temp->left == node_to_find or temp->right == node_to_find) check = true;
else
{
if(temp->left) q.push(temp->left);
if(temp->right) q.push(temp->right);
}
}
if(check)
{
while(!q.empty())
{
res.push_back(q.front()->data);
q.pop();
}
break;
}
}
if(res.empty()) res.push_back(-1);
return res;
}