Given two binary search trees root1 and root2, return a list containing all the integers from both trees sorted in ascending order.

&nbsp;
Example 1:

Input: root1 = [2,1,4], root2 = [1,0,3]
Output: [0,1,1,2,3,4]


Example 2:

Input: root1 = [1,null,8], root2 = [8,1]
Output: [1,1,8,8]


&nbsp;
Constraints:


	The number of nodes in each tree is in the range [0, 5000].
	-105 &lt;= Node.val &lt;= 105


# 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]:
        
        
        l1,l2=[],[]
        def preorder(root,l):
            if root is None:
                return 
            preorder(root.left,l)
            l.append(root.val)
            preorder(root.right,l)
        preorder(root1,l1)
        preorder(root2,l2)
        return sorted(l1+l2)
class Solution {
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        Stack<TreeNode> st1 = new Stack<>();
        Stack<TreeNode> st2 = new Stack<>();
        
        List<Integer> res = new ArrayList<>();
        
        while(root1 != null || root2 != null || !st1.empty() || !st2.empty()){
            while(root1 != null){
                st1.push(root1);
                root1 = root1.left;
            }
            while(root2 != null){
                st2.push(root2);
                root2 = root2.left;
            }
            if(st2.empty() || (!st1.empty() && st1.peek().val <= st2.peek().val)){
                root1 = st1.pop();
                res.add(root1.val);
                root1 = root1.right;
            }
            else{
                root2 = st2.pop();
                res.add(root2.val);
                root2 = root2.right;
            }
        }
        return res;
    }
}
class Solution {
public:
    vector<int> v;
    void Temp(TreeNode* root)
    {
        if(root!=NULL){
        Temp(root->left);
        v.push_back(root->val);
        Temp(root->right);
        }
    }
    vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
        Temp(root1);
        Temp(root2);
        sort(v.begin(), v.end());

        return v;
    }
};
var getAllElements = function(root1, root2) {
    const ans = [];
    const traverse = (r) => {
        if(!r) return;
        traverse(r.left);
        ans.push(r.val);
        traverse(r.right);
    }
    traverse(root1);
    traverse(root2);
    return ans.sort((a, b) => a - b);
};
All Elements in Two Binary Search Trees