1305. All elements in two binary search tree

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

  1. What is the search tree?
  2. 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.
  3. Therefore, the first step is to use in-order traverse to get the first and second binary tree’s all elements.
  4. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值