Skip to content

Commit 90eb0b0

Browse files
committed
leetcode
1 parent 08160cf commit 90eb0b0

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public:
3+
4+
void inorder(TreeNode* root,vector<int> &v)
5+
{
6+
if(!root)
7+
return;
8+
9+
inorder(root->left,v);
10+
v.push_back(root->val);
11+
inorder(root->right,v);
12+
}
13+
14+
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
15+
16+
vector<int> t1;
17+
vector<int> t2;
18+
19+
inorder(root1,t1);
20+
inorder(root2,t2);
21+
22+
vector<int> ans;
23+
int i=0,j=0;
24+
25+
while(i<t1.size() && j<t2.size())
26+
{
27+
if(t1[i]<t2[j])
28+
{
29+
ans.push_back(t1[i++]);
30+
}
31+
else
32+
{
33+
ans.push_back(t2[j++]);
34+
}
35+
}
36+
37+
while(j<t2.size())
38+
{
39+
ans.push_back(t2[j++]);
40+
}
41+
42+
while(i<t1.size())
43+
{
44+
ans.push_back(t1[i++]);
45+
}
46+
return ans;
47+
}
48+
};

0 commit comments

Comments
 (0)