<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.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:
        def traverse(root):
            res = []
            if root.left:
                res.extend(traverse(root.left))
            res.append(root.val)
            if root.right:
                res.extend(traverse(root.right))
            return res
        l1 = traverse(root1) if root1 else []
        l2 = traverse(root2) if root2 else []
        
        res = []
        i=0
        j=0
        while i<len(l1) and j<len(l2):
            if l1[i] < l2[j]:
                res.append(l1[i])
                i+=1
            else:
                res.append(l2[j])
                j+=1
        if i<len(l1):
            res.extend(l1[i:])
        if j<len(l2):
            res.extend(l2[j:])
        return res
        
        # gen1 = traverse(root1) if root1 is not None else (i for i in range(0))
        # gen2 = traverse(root2)if root2 is not None else (i for i in range(0))
        # # print(list(traverse(root1)))
        # # print(list(traverse(root2)))
        # res = []
        # try:
        #     n1 = next(gen1)
        # except StopIteration:
        #     n1 = None
        # try:
        #     n2 = next(gen2)
        # except StopIteration:
        #     n2 = None
        # while n1 is not None and n2 is not None:
        #     # print(n1, n2)
        #     if n1 < n2:
        #         res.append(n1)
        #         try:
        #             n1 = next(gen1)
        #         except StopIteration:
        #             n1 = None
        #     else:
        #         res.append(n2)
        #         try:
        #             n2 = next(gen2)
        #         except StopIteration:
        #             n2 = None
        # if n1 is not None:
        #     res.append(n1)
        #     res.extend(list(gen1))
        # if n2 is not None:
        #     res.append(n2)
        #     res.extend(list(gen2))
        # return res
20600