-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path1305-kir3i.cpp
47 lines (42 loc) · 1.25 KB
/
1305-kir3i.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> ans, t1, t2;
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
int i1=0, i2=0;
popAscList(root1, t1);
popAscList(root2, t2);
while(i1<t1.size() && i2<t2.size()) {
if (t1[i1] <= t2[i2])
ans.push_back(t1[i1++]);
else
ans.push_back(t2[i2++]);
}
if (i1 == t1.size())
ans.insert(ans.end(), t2.begin()+i2, t2.end());
else
ans.insert(ans.end(), t1.begin()+i1, t1.end());
return ans;
}
void popAscList(TreeNode* root, vector<int> &t) {
if (!root) {
t = {};
return;
}
if (root->left)
popAscList(root->left, t);
t.push_back(root->val);
if (root->right)
popAscList(root->right, t);
}
};