The description of problem
Given two binary search trees root1 and root2, return a list containing all the integers from both trees sorted in ascending order.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees
example
The intuition for this problem
- What is the search tree?
- I just tell you that when you traverse a search tree in an in-order way, you will get an array of value in ascending order.
- Therefore, the first step is to use in-order traverse to get the first and second binary tree’s all elements.
- Then use the merge function to merge two sorted arrays.
Code 1
#include <vector>
#include <iostream>
using namespace std;
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) {
vector<int> res_1;
vector<int> res_2;
// inorder traversal root1 and root2 to get the sorted array into res_1 and res_2, respectively
inorder(root1, res_1);
inorder(root2, res_2);
// merge res_1 and res_2
vector<int> res;
size_t i = 0;
size_t j = 0;
while (i < res_1.size() && j < res_2.size()) {
if (res_1[i] < res_2[j]) {
res.push_back(res_1[i]);
i++;
} else {
res.push_back(res_2[j]);
j++;
}
}
while (i < res_1.size()) {
res.push_back(res_1[i]);
i++;
}
while (j < res_2.size()) {
res.push_back(res_2[j]);
j++;
}
return res;
}
void inorder(TreeNode *root, vector<int> &res) {
if (root == nullptr)
return;
inorder(root->left, res);
res.emplace_back(root->val);
inorder(root->right, res);
}
};
int main()
{
// generate a search binary tree with root1 = [2,1,4]
TreeNode *root1 = new TreeNode(2);
root1->left = new TreeNode(1);
root1->right = new TreeNode(4);
// generate a search binary tree with root2 = [1,0,3]
TreeNode *root2 = new TreeNode(1);
root2->left = new TreeNode(0);
root2->right = new TreeNode(3);
Solution _s;
vector<int> res = _s.getAllElements(root1, root2);
for (auto i : res) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
Code 2
#include <vector>
#include <iostream>
using namespace std;
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) {
vector<int> res_1;
vector<int> res_2;
// inorder traversal root1 and root2 to get the sorted array into res_1 and res_2, respectively
inorder(root1, res_1);
inorder(root2, res_2);
// merge res_1 and res_2
vector<int> res;
while (res_1.begin() != res_1.end() && res_2.begin() != res_2.end()) {
if (res_1.front() < res_2.front()) {
res.emplace_back(res_1.front());
res_1.erase(res_1.begin());
} else {
res.emplace_back(res_2.front());
res_2.erase(res_2.begin());
}
}
if (res_1.begin() != res_1.end()) {
res.insert(res.end(), res_1.begin(), res_1.end());
}
if (res_2.begin() != res_2.end()) {
res.insert(res.end(), res_2.begin(), res_2.end());
}
return res;
}
void inorder(TreeNode *root, vector<int> &res) {
if (root == nullptr)
return;
inorder(root->left, res);
res.emplace_back(root->val);
inorder(root->right, res);
}
};
int main()
{
// generate a search binary tree with root1 = [2,1,4]
TreeNode *root1 = new TreeNode(2);
root1->left = new TreeNode(1);
root1->right = new TreeNode(4);
// generate a search binary tree with root2 = [1,0,3]
TreeNode *root2 = new TreeNode(1);
root2->left = new TreeNode(0);
root2->right = new TreeNode(3);
Solution _s;
vector<int> res = _s.getAllElements(root1, root2);
for (auto i : res) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
The corresponding results
0 1 1 2 3 4