From 11ac67274bb0000c8c60e0f4fe2309d2d7f39d7b Mon Sep 17 00:00:00 2001 From: eat-swap Date: Sun, 1 May 2022 01:29:14 +0800 Subject: [PATCH] add: 220501-CN [cpp] --- cpp/2205/220501-CN.cpp | 117 ++++++++++++++++++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 6 +++ cpp/CMakeLists.txt | 3 +- 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220501-CN.cpp create mode 100644 cpp/2205/CMakeLists.txt diff --git a/cpp/2205/220501-CN.cpp b/cpp/2205/220501-CN.cpp new file mode 100644 index 0000000..b1aca90 --- /dev/null +++ b/cpp/2205/220501-CN.cpp @@ -0,0 +1,117 @@ +#include +#include + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + + explicit TreeNode(int x = 0, TreeNode* l = nullptr, TreeNode* r = nullptr) : val(x), left(l), right(r) {} +}; + +/** + * BSTIterator + * + * Copied from 220420.cpp + * Refer: 173. Binary Search Tree BSTIterator + */ + +class BSTIterator { +private: + std::stack s; + +public: + explicit BSTIterator(const TreeNode* root) { + for (; root; root = root->left) + s.push(root); + } + + int next() { + if (s.empty()) + return -1; + const TreeNode* ret = s.top(); + s.pop(); + if (ret->right) + for (const TreeNode* ptr = ret->right; ptr; ptr = ptr->left) + s.push(ptr); + return ret->val; + } + + bool hasNext() { + return !s.empty(); + } +}; + +/** + * PeekingBSTIterator + * + * Copied & modified from 220425.cpp [PeekingIterator] + * Refer: 284. Peeking Iterator + */ + +class PeekingBSTIterator : public BSTIterator { +private: + int x = 0; + bool isEmpty = false; + +public: + explicit PeekingBSTIterator(const TreeNode* nums) : BSTIterator(nums) { + // Initialize any member here. + // **DO NOT** save a copy of nums and manipulate it directly. + // You should only use the BSTIterator interface methods. + if (!BSTIterator::hasNext()) + isEmpty = true; + else + this->x = BSTIterator::next(); + } + + // Returns the next element in the iteration without advancing the BSTIterator. + int peek() const { + return x; + } + + // hasNext() and next() should behave the same as in the BSTIterator interface. + // Override them if needed. + int next() { + if (this->isEmpty) + return -1; + auto ret = this->x; + + if (BSTIterator::hasNext()) { + this->x = BSTIterator::next(); + } else { + this->isEmpty = true; + } + + return ret; + } + + bool hasNext() const { + return !this->isEmpty; + } +}; + +/** + * 1305. All Elements in Two Binary Search Trees + * Given two binary search trees root1 and root2, return a list containing all the integers from both trees sorted in ascending order. + * + * Alternative refer: 220126.cpp (100% STL implementation) + */ + +class Solution { +public: + static std::vector getAllElements(TreeNode* root1, TreeNode* root2) { + auto* it1 = new PeekingBSTIterator(root1); + auto* it2 = new PeekingBSTIterator(root2); + + std::vector ret; + while (it1->hasNext() && it2->hasNext()) + ret.push_back((it1->peek() < it2->peek() ? it1 : it2)->next()); + while (it1->hasNext()) + ret.push_back(it1->next()); + while (it2->hasNext()) + ret.push_back(it2->next()); + + return ret; + } +}; \ No newline at end of file diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt new file mode 100644 index 0000000..29cccfd --- /dev/null +++ b/cpp/2205/CMakeLists.txt @@ -0,0 +1,6 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.21) +PROJECT(2205) + +SET(CMAKE_CXX_STANDARD 23) + +ADD_EXECUTABLE(2204 220501-CN.cpp) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index a2ee3f6..8e70199 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -12,5 +12,6 @@ ADD_EXECUTABLE(leetcode-cpp main.cpp) # ADD_SUBDIRECTORY(2201) # ADD_SUBDIRECTORY(2202) # ADD_SUBDIRECTORY(2203) -ADD_SUBDIRECTORY(2204) +# ADD_SUBDIRECTORY(2204) +ADD_SUBDIRECTORY(2205) ADD_SUBDIRECTORY(more)