add: 220126
This commit is contained in:
parent
1cfe42d38e
commit
f0d28defc1
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2201)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2201 220126-CN.cpp)
|
||||
ADD_EXECUTABLE(2201 220126.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue