1. /**
    2. * Definition for a binary tree node.
    3. * function TreeNode(val, left, right) {
    4. * this.val = (val===undefined ? 0 : val)
    5. * this.left = (left===undefined ? null : left)
    6. * this.right = (right===undefined ? null : right)
    7. * }
    8. */
    9. /**
    10. * @param {TreeNode} root1
    11. * @param {TreeNode} root2
    12. * @return {number[]}
    13. */
    14. var getAllElements = function(root1, root2) {
    15. let arr_left = []
    16. let arr_right = []
    17. let dfs = (data, arr) => {
    18. if (data) {
    19. dfs(data.left, arr)
    20. dfs(data.right, arr)
    21. arr.push(data.val)
    22. }
    23. }
    24. dfs(root1, arr_left)
    25. dfs(root2, arr_right)
    26. return arr_left.concat(arr_right).sort((a, b) => {return a - b})
    27. };

    image.png