1305. All Elements in Two Binary Search Trees

1305. All Elements in Two Binary Search Trees

Description

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

Example 1:

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

Example 2:

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

Constraints:

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

Hints/Notes

  • 2024/07-08
  • get two sorted array first
  • no solution fromm 0x3F

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
auto v1 = getNums(root1);
auto v2 = getNums(root2);
int idx1 = 0, idx2 = 0;
vector<int> res;
while (idx1 < v1.size() || idx2 < v2.size()) {
if (idx1 == v1.size()) {
res.push_back(v2[idx2++]);
} else if (idx2 == v2.size()) {
res.push_back(v1[idx1++]);
} else if (v1[idx1] < v2[idx2]) {
res.push_back(v1[idx1++]);
} else {
res.push_back(v2[idx2++]);
}
}
return res;
}

vector<int> getNums(TreeNode* root) {
vector<int> res;
if (!root) {
return {};
}
res.push_back(root->val);
auto l = getNums(root->left);
auto r = getNums(root->right);
if (!l.empty()) {
res.insert(res.begin(), l.begin(), l.end());
}
if (!r.empty()) {
res.insert(res.end(), r.begin(), r.end());
}
return res;
}
};