From f0d28defc158b49f160f03e74891d7830048bd91 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Thu, 27 Jan 2022 15:29:38 +0800 Subject: [PATCH] add: 220126 --- 2201/220126.cpp | 33 +++++++++++++++++++++++++++++++++ 2201/CMakeLists.txt | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 2201/220126.cpp diff --git a/2201/220126.cpp b/2201/220126.cpp new file mode 100644 index 0000000..56096a9 --- /dev/null +++ b/2201/220126.cpp @@ -0,0 +1,33 @@ +#include + +/** + * 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& ret) { + if (root->left) + dfs(root->left, ret); + ret.push_back(root->val); + if (root->right) + dfs(root->right, ret); + } +public: + static std::vector getAllElements(const TreeNode* const root1, const TreeNode* const root2) { + std::vector 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; + } +}; \ No newline at end of file diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index e96eb06..7bfcfe6 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220126-CN.cpp) +ADD_EXECUTABLE(2201 220126.cpp)