-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1305.cpp
More file actions
46 lines (46 loc) · 1.27 KB
/
Copy path1305.cpp
File metadata and controls
46 lines (46 loc) · 1.27 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
/**
* 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:
void get_list(TreeNode* root, vector<int>& res){
if(!root)
return;
get_list(root->left,res);
res.push_back(root->val);
get_list(root->right,res);
return;
}
vector<int> merge(vector<int>& list1,vector<int>& list2){
int n1 = list1.size(), n2=list2.size();
vector<int> res(n1+n2);
int i=0,j=0,k=0;
while(i<n1 && j<n2){
if(list1[i]<=list2[j])
res[k++] = list1[i++];
else
res[k++] = list2[j++];
}
while(i<n1){
res[k++] = list1[i++];
}
while(j<n2){
res[k++] = list2[j++];
}
return res;
}
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> list1,list2;
get_list(root1,list1);
get_list(root2,list2);
return merge(list1,list2);
}
};