首先用最直观的解法,先将两个binary search tree的元素插入到vector,然后排序。

/**
 * 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) {
        vector<int> res;
        visit(root1,res);
        visit(root2,res);
        sort(res.begin(),res.end());
        return res;
    }
private:
    void visit(TreeNode* root,vector<int> & res) {
        if(!root) return;
        res.push_back(root->val);
        visit(root->left,res);
        visit(root->right,res);
    }
};

运行效率:

Runtime: 136 ms, faster than 53.95% of C++ online submissions for All Elements in Two Binary Search Trees.
Memory Usage: 84.3 MB, less than 89.60% of C++ online submissions for All Elements in Two Binary Search Trees.

然而题目中给的二叉搜索树,中序遍历已经是有序的,是不是可以优化成两个有序的vector合并成一个大的有序的vector(好像也有到这题?)

/**
 * 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) {
        vector<int> tree1,tree2;
        visit(root1,tree1);
        visit(root2,tree2);
        vector<int> res(tree1.size() + tree2.size());
        merge(tree1,tree2,res);
        return res;
    }
private:
    void merge(const vector<int>& vec1,const vector<int>& vec2,vector<int>& res)
    {
        int i = 0,j = 0,k = 0;
        while(i < vec1.size() && j < vec2.size())
        {
            if(vec1[i] < vec2[j])
                res[k++] = vec1[i++];
            else
                res[k++] = vec2[j++];
        }
        
        while(i < vec1.size())
            res[k++] = vec1[i++];
        
        while(j < vec2.size())
            res[k++] = vec2[j++];
    }
    void visit(TreeNode* root,vector<int> & res) {
        if(!root) return;
        visit(root->left,res);
        res.push_back(root->val);
        visit(root->right,res);
    }
};

程序执行效率:

Runtime: 132 ms, faster than 67.03% of C++ online submissions for All Elements in Two Binary Search Trees.
Memory Usage: 85.2 MB, less than 55.23% of C++ online submissions for All Elements in Two Binary Search Trees.

看了下提交最快的代码发现与我的代码基本一致,但是多了如下代码:

ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);

我的代码添加后:

/**
 * 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) {
        vector<int> tree1,tree2;
        visit(root1,tree1);
        visit(root2,tree2);
        vector<int> res(tree1.size() + tree2.size());
        merge(tree1,tree2,res);
        return res;
    }
private:
    void merge(const vector<int>& vec1,const vector<int>& vec2,vector<int>& res)
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        int i = 0,j = 0,k = 0;
        while(i < vec1.size() && j < vec2.size())
        {
            if(vec1[i] < vec2[j])
                res[k++] = vec1[i++];
            else
                res[k++] = vec2[j++];
        }
        
        while(i < vec1.size())
            res[k++] = vec1[i++];
        
        while(j < vec2.size())
            res[k++] = vec2[j++];
    }
    void visit(TreeNode* root,vector<int> & res) {
        if(!root) return;
        visit(root->left,res);
        res.push_back(root->val);
        visit(root->right,res);
    }
};

运行效率:

Runtime: 104 ms, faster than 99.09% of C++ online submissions for All Elements in Two Binary Search Trees.
Memory Usage: 85.5 MB, less than 43.13% of C++ online submissions for All Elements in Two Binary Search Trees.

关于sync_with_stdio(false)等参考stackoverflow

["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]