-
Notifications
You must be signed in to change notification settings - Fork 481
/
1305.cpp
34 lines (33 loc) · 861 Bytes
/
1305.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
class Solution
{
public:
vector<int> getAllElements(TreeNode* root1, TreeNode* root2)
{
queue<int> q1, q2;
vector<int> res;
inOrder(root1, q1);
inOrder(root2, q2);
while (!q1.empty() || !q2.empty())
{
if (q1.empty() || (!q2.empty() && q2.front() <= q1.front()))
{
res.emplace_back(q2.front());
q2.pop();
}
else if (q2.empty() || (!q1.empty() && q1.front() < q2.front()))
{
res.emplace_back(q1.front());
q1.pop();
}
}
return res;
}
private:
void inOrder(TreeNode* root, queue<int>& q)
{
if (root == nullptr) return;
inOrder(root->left, q);
q.emplace(root->val);
inOrder(root->right, q);
}
};