<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" />
<pre>
<strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3]
<strong>Output:</strong> [0,1,1,2,3,4]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" />
<pre>
<strong>Input:</strong> root1 = [1,null,8], root2 = [8,1]
<strong>Output:</strong> [1,1,8,8]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li>
	<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

/**
 * 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 inorder_traversal(TreeNode* root,vector<int>&res){
     if(root==NULL){
        return;
    }
    stack<TreeNode*>s;
    TreeNode *curr=root;
    while(curr!=NULL||s.empty()==false){
        while(curr!=NULL){
            s.push(curr);
            curr=curr->left;
        }
        res.push_back(s.top()->val);
        curr=s.top()->right;
        s.pop();
    }
}

vector<int>sorted_merge_two_arrays(vector<int>&a,vector<int>&b){
    if(a.size()==0){
        return b;
    }
    if(b.size()==0){
        return a;
    }
    vector<int>res;
    int i=0,j=0;
    while(i<a.size()&&j<b.size()){
        if(a[i]<b[j]){
            res.push_back(a[i]);
            i++;
        }
        else{
            res.push_back(b[j]);
            j++;
        }
    }
    while(i<a.size()){
        res.push_back(a[i]);
        i++;
    }
    while(j<b.size()){
        res.push_back(b[j]);
        j++;
    }
    return res;

}

vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
        vector<int>r1,r2;
        inorder_traversal(root1,r1);
        inorder_traversal(root2,r2);
        return sorted_merge_two_arrays(r1,r2);

}
};
85600