-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_MergeTwoBST.cpp
43 lines (41 loc) · 1.06 KB
/
GFG_MergeTwoBST.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
/*
https://practice.geeksforgeeks.org/problems/merge-two-bst-s/1
Merge two BST 's
*/
class Solution
{
public:
//Function to return a list of integers denoting the node
//values of both the BST in a sorted order.
void goleft(Node* root, stack<Node*> &st)
{
while(root)
{
st.push(root);
root = root->left;
}
}
vector<int> merge(Node *root1, Node *root2)
{
stack<Node*> s1, s2;
vector<int> ans;
goleft(root1, s1);
goleft(root2, s2);
while(!s1.empty() || !s2.empty())
{
int a = s1.size() ? s1.top()->data : INT_MAX;
int b = s2.size() ? s2.top()->data : INT_MAX;
if(a<=b){
ans.push_back(a);
Node* t = s1.top(); s1.pop();
if(t->right) goleft(t->right, s1);
}
else{
ans.push_back(b);
Node* t = s2.top(); s2.pop();
if(t->right) goleft(t->right, s2);
}
}
return ans;
}//
};