-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsiblings.cpp
More file actions
60 lines (43 loc) · 1.42 KB
/
siblings.cpp
File metadata and controls
60 lines (43 loc) · 1.42 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
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(a, b, i) for (int i = a; i < b; i++)
#define rrep(a, b, i) for (int i = a; i >= b; i--)
#define m 1000000007
struct node{
int val;
node* left;
node* right;
node(int val):val(val){}
};
pair<int, node*> isSiblingHelper(node* head,node *child){
queue<pair<node*,pair<int,node*>>> q;
q.push({head,{0,nullptr}});
while(!q.empty()){
pair<node*,pair<int,node*>> top = q.front();
q.pop();
if(child==top.first){
return top.second;
}
if(top.first->left)q.push({top.first->left,make_pair(top.second.first+1,top.first)});
if(top.first->right)q.push({top.first->right,{top.second.first+1,top.first}});
}
return {-1,nullptr};
}
bool isSiblings(node* head, node* child1, node* child2){
pair<int, node*> outChild1 = isSiblingHelper(head,child1);
pair<int, node*> outChild2 = isSiblingHelper(head,child2);
if(outChild1.first == outChild2.first && outChild1.second!= outChild2.second)return true;
return false;
}
int main(){
node* head = new node(1);
head->left = new node(2);
head->right = new node(3);
head->left->left = new node(2);
head->left->right = new node(5);
head->right->left = new node(6);
head->right->right = new node(9);
cout<<isSiblings(head, head->right->right,head->right->right)<<"\n";
return 0;
}