-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsibling_nodes.cpp
More file actions
48 lines (37 loc) · 1.01 KB
/
Copy pathsibling_nodes.cpp
File metadata and controls
48 lines (37 loc) · 1.01 KB
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
// problems_link : https://www.geeksforgeeks.org/problems/print-all-nodes-that-dont-have-sibling/1
vector<int> noSibling(Node* root)
{
vector<int>ans ;
set<int>ans2 ;
if(!root){
return ans ;
}
queue<Node *>q ;
q.push(root) ;
while(!q.empty()){
int n = q.size() ;
for(int i = 0 ; i<n ; i++){
Node *temp = q.front() ;
if(temp->left && temp->right){
q.push(temp->left) ;
q.push(temp->right) ;
}
else if(temp->right) {
q.push(temp->right) ;
ans2.insert(temp->right->data) ;
}
else if(temp->left){
q.push(temp->left) ;
ans2.insert(temp->left->data) ;
}
q.pop() ;
}
}
if(ans2.size()==0){
return {-1} ;
}
for(auto it : ans2){
ans.push_back(it) ;
}
return ans ;
}