add: 220126

This commit is contained in:
Lam Haoyin 2022-01-27 15:29:38 +08:00
parent 1cfe42d38e
commit f0d28defc1
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 34 additions and 1 deletions

33
2201/220126.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <vector>
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
explicit TreeNode(int x = 0, TreeNode* left = nullptr, TreeNode* right = nullptr) : val(x), left(left), right(right) {}
};
class Solution {
private:
static void dfs(const TreeNode* const root, std::vector<int>& ret) {
if (root->left)
dfs(root->left, ret);
ret.push_back(root->val);
if (root->right)
dfs(root->right, ret);
}
public:
static std::vector<int> getAllElements(const TreeNode* const root1, const TreeNode* const root2) {
std::vector<int> d1, d2, ret;
if (root1)
dfs(root1, d1);
if (root2)
dfs(root2, d2);
std::merge(d1.begin(), d1.end(), d2.begin(), d2.end(), std::back_inserter(ret));
return ret;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2201)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2201 220126-CN.cpp) ADD_EXECUTABLE(2201 220126.cpp)