-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrothers From Different Roots
61 lines (55 loc) Β· 1.48 KB
/
Brothers From Different Roots
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
61
class Solution
{
public:
int countPairs(Node* root1, Node* root2, int x)
{
//see as we know the condition can arise when we will be having sum i.e a+b>x || a+b<x
//so we should be doing some stuff so that when situation comes we can increase or decrease
stack<Node*>s1;
stack<Node*>s2;
int ans=0;
Node*a=root1;
Node*b=root2;
while(true)
{
while(a)
{
s1.emplace(a);
a=a->left;
}
while(b)
{
s2.emplace(b);
b=b->right;
}
//if at any point the any of the stack is empty then we should break
if(s1.empty() || s2.empty())
{
break;
}
auto atop=s1.top();
auto btop=s2.top();
int sum=atop->data+btop->data;
if(sum==x)
{
ans++;
s1.pop();
s2.pop();
a=atop->right;
b=btop->left;
}
else if(sum<x)
{
//means we should increase the sum which implies should increase the value from that bst from which we were taking small values
s1.pop();
a=atop->right;
}
else
{
s2.pop();
b=btop->left;
}
}
return ans;
}
};