first and second. In each tree, every value in a node's left subtree is no greater than the node's value, and every value in its right subtree is no smaller.
Return a single list with the values of all nodes from both trees in non-decreasing order. A value held by several nodes appears in the answer that many times.
Either tree may be empty. Try to avoid a full sort and use the fact that both trees are already ordered.first=tree_node([6, 2, 9]), second=tree_node([4, 1, 7]) → [1, 2, 4, 6, 7, 9]
The first tree holds 2, 6, 9 and the second holds 1, 4, 7. Together in ascending order: 1, 2, 4, 6, 7, 9.
first=tree_node([3, None, 5]), second=tree_node([5, 3]) → [3, 3, 5, 5]
Duplicates stay: each value appears as many times as there are nodes holding it.
first=tree_node([]), second=tree_node([-8]) → [-8]
The first tree is empty, so the answer is just the second tree.
Node.val ≤ 10⁵first=tree_node([6, 2, 9]), second=tree_node([4, 1, 7])
[1, 2, 4, 6, 7, 9]
Run checks the first 3 tests in your browser. Submit checks all 51 tests on the server, hidden ones included.