All Elements in Two Binary Search Trees

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

Example 1
        2             1
       / \           / \
      1   4         0   3
Inputroot1 = [2,1,4], root2 = [1,0,3]
Output[0,1,1,2,3,4]
The values from both trees are 2, 1, 4, 1, 0, and 3, which sorted in ascending order are [0, 1, 1, 2, 3, 4].
Example 2
        1           8
         \         /
          8       1
Inputroot1 = [1,null,8], root2 = [8,1]
Output[1,1,8,8]
The values from both trees are 1, 8, 8, and 1, which sorted in ascending order are [1, 1, 8, 8].

Constraints

  • The number of nodes in each tree is in the range [0, 5000].
  • -10^5 <= Node.val <= 10^5

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate