diff --git a/cpp/2303/230316.cpp b/cpp/2303/230316.cpp new file mode 100644 index 0000000..c216397 --- /dev/null +++ b/cpp/2303/230316.cpp @@ -0,0 +1,42 @@ +#include + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + + explicit TreeNode(int x, TreeNode* left = nullptr, TreeNode* right = nullptr) : val(x), left(left), right(right) {} +}; + +/** + * 106. Construct Binary Tree from Inorder and Postorder Traversal + * + * Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree. + */ + +using VCI = std::vector::const_iterator; + +class Solution { +private: + static TreeNode* bT(VCI is, VCI ie, VCI ps, VCI pe) { + if (is == ie || ps == pe) + return nullptr; + int r = *(pe - 1); + int pos = std::find(is, ie, r) - is; + return new TreeNode( + r, + bT(is, is + pos, ps, ps + pos), + bT(is + pos + 1, ie, ps + pos, pe - 1) + ); + } + +public: + static TreeNode* buildTree(const std::vector& inorder, const std::vector& postorder) { + return bT(inorder.begin(), inorder.end(), postorder.begin(), postorder.end()); + } +}; + +int main() { + Solution::buildTree({-1}, {-1}); + return 0; +} diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index f40afe4..1228d50 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230316-CN.cpp) +ADD_EXECUTABLE(2303 230316.cpp)