<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 dfs_inorder_1(TreeNode* root1, vector<int> &ans1){
        if(!root1) return;
        dfs_inorder_1(root1->left, ans1);
        ans1.push_back(root1->val);
        cout<< " "<< root1->val;
        dfs_inorder_2(root1->right, ans1);
    }

    void dfs_inorder_2(TreeNode* root2, vector<int> &ans2){
        if(!root2) return;
        dfs_inorder_1(root2->left, ans2);
        ans2.push_back(root2->val);
        cout<< " "<< root2->val;
        dfs_inorder_2(root2->right, ans2);
    }

    vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        vector<int> ans1, ans2, ans3 ;
        // vector<int> ans2 ;
        // vector<int> ans3 ;
        dfs_inorder_1(root1, ans1);cout<<endl;
        dfs_inorder_2(root2, ans2);
        int i1= 0, i2= 0;

        while(i1 < ans1.size() || i2 < ans2.size()){
            if(i1 == ans1.size()) {ans3.push_back(ans2[i2]);
                i2++;}
            else if(i2 == ans2.size()) {ans3.push_back(ans1[i1]);
                i1++;}    
            else if(ans1[i1]<ans2[i2]){
                ans3.push_back(ans1[i1]);
                i1++;
            }
            else{
                ans3.push_back(ans2[i2]);
                i2++;
            }
        } 
        return ans3;
    }
};
85700