``````/**
* 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> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> res;
visit(root1,res);
visit(root2,res);
sort(res.begin(),res.end());
return res;
}
private:
void visit(TreeNode* root,vector<int> & res) {
if(!root) return;
res.push_back(root->val);
visit(root->left,res);
visit(root->right,res);
}
};``````

``````Runtime: 136 ms, faster than 53.95% of C++ online submissions for All Elements in Two Binary Search Trees.
Memory Usage: 84.3 MB, less than 89.60% of C++ online submissions for All Elements in Two Binary Search Trees.``````

``````/**
* 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> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> tree1,tree2;
visit(root1,tree1);
visit(root2,tree2);
vector<int> res(tree1.size() + tree2.size());
merge(tree1,tree2,res);
return res;
}
private:
void merge(const vector<int>& vec1,const vector<int>& vec2,vector<int>& res)
{
int i = 0,j = 0,k = 0;
while(i < vec1.size() && j < vec2.size())
{
if(vec1[i] < vec2[j])
res[k++] = vec1[i++];
else
res[k++] = vec2[j++];
}

while(i < vec1.size())
res[k++] = vec1[i++];

while(j < vec2.size())
res[k++] = vec2[j++];
}
void visit(TreeNode* root,vector<int> & res) {
if(!root) return;
visit(root->left,res);
res.push_back(root->val);
visit(root->right,res);
}
};``````

``````Runtime: 132 ms, faster than 67.03% of C++ online submissions for All Elements in Two Binary Search Trees.
Memory Usage: 85.2 MB, less than 55.23% of C++ online submissions for All Elements in Two Binary Search Trees.``````

``````ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);``````

``````/**
* 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> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> tree1,tree2;
visit(root1,tree1);
visit(root2,tree2);
vector<int> res(tree1.size() + tree2.size());
merge(tree1,tree2,res);
return res;
}
private:
void merge(const vector<int>& vec1,const vector<int>& vec2,vector<int>& res)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int i = 0,j = 0,k = 0;
while(i < vec1.size() && j < vec2.size())
{
if(vec1[i] < vec2[j])
res[k++] = vec1[i++];
else
res[k++] = vec2[j++];
}

while(i < vec1.size())
res[k++] = vec1[i++];

while(j < vec2.size())
res[k++] = vec2[j++];
}
void visit(TreeNode* root,vector<int> & res) {
if(!root) return;
visit(root->left,res);
res.push_back(root->val);
visit(root->right,res);
}
};``````

``````Runtime: 104 ms, faster than 99.09% of C++ online submissions for All Elements in Two Binary Search Trees.
Memory Usage: 85.5 MB, less than 43.13% of C++ online submissions for All Elements in Two Binary Search Trees.``````

["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]