<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 NodeStack:
    def __init__(self, root):
        # self.rootval = root.val
        if not root:
            self.stack = []
        else:
            self.stack = [root]
            while self.stack[-1].left:
                self.stack.append(self.stack[-1].left)
        # print("postinit", self.stack)
    
    def get_next(self):
        # print(self.rootval, self.stack)
        if self.stack:
            curr = self.stack.pop()
            if curr.right:
                self.stack.append(curr.right)
                while self.stack[-1].left:
                    self.stack.append(self.stack[-1].left)
            # print("returning with", self.stack[-1].val)
            return curr.val
        else:
            return None


class Solution:
    def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:
        ns1 = NodeStack(root1)
        ns2 = NodeStack(root2)
        res = []
        n1 = ns1.get_next()
        n2 = ns2.get_next()
        # counter = 0
        # print("her")
        while n1 is not None and n2 is not None:
            # print(n1, n2)
            # counter+=1
            # if counter>20:
            #     return []
            if n1<n2:   
                res.append(n1)
                n1 = ns1.get_next()
            elif n2<n1:
                res.append(n2)
                n2 = ns2.get_next()
            else:
                res.append(n1)
                res.append(n2)
                n1 = ns1.get_next()
                n2 = ns2.get_next()
        while n1 is not None:
            res.append(n1)
            n1 = ns1.get_next()
        while n2 is not None:
            res.append(n2)
            n2 = ns2.get_next()
        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
20000