给你 root1 和 root2 这两棵二叉搜索树。请你返回一个列表,其中包含 两棵树 中的所有整数并按 升序 排序。.
示例 1:
输入:root1 = [2,1,4], root2 = [1,0,3]
输出:[0,1,1,2,3,4]
示例 2:
输入:root1 = [1,null,8], root2 = [8,1]
输出:[1,1,8,8]
class Solution {
public List getAllElements(TreeNode root1, TreeNode root2) {
List list=new ArrayList<>();
getAll(list,root1);
getAll(list,root2);
Collections.sort(list);
return list;
}
public void getAll(List list,TreeNode root){
if(root==null){
return;
}
list.add(root.val);
getAll(list,root.left);
getAll(list,root.right);
}
}
func getAllElements(root1 *TreeNode, root2 *TreeNode) []int {
list := make([]int, 0)
getAll(&list,root1)
getAll(&list,root2)
sort.Ints(list)
return list
}
func getAll(list *[]int,root *TreeNode){
if root==nil{
return;
}
*list=append(*list,root.Val)
getAll(list,root.Left)
getAll(list,root.Right)
}



