-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAllElements.cpp
41 lines (41 loc) · 820 Bytes
/
getAllElements.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
#include<iostream>
#include<vector>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<int> inorder(TreeNode* root,vector<int> r)
{
if(root)
{
r=inorder(root->left,r);
r.push_back(root->val);
r=inorder(root->right,r);
}
return r;
}
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> r1,r2,ret;
r1=postorder(root1,r1);
r2=postorder(root2,r2);
int r=max(r1.at(r1.size()-1),r2.at(r2.size()-1));
r1.push_back(r+1);
r2.push_back(r+1);
int i=j=0;
while(ret.size()<=(r1.size()+r2.size()-2))
{
if(r1[i]<r2[j])
{
ret.push_back(r1[i]);
++i;
}
else
{
ret.push_back(r2[j]);
++j;
}
}
return ret;
}