diff --git a/cpp/2205/220517-CN.cpp b/cpp/2205/220517-CN.cpp index 81b1a82..95c50f2 100644 --- a/cpp/2205/220517-CN.cpp +++ b/cpp/2205/220517-CN.cpp @@ -2,6 +2,12 @@ #include #include +/** + * 953. Verifying an Alien Dictionary + * In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. + * Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language. + */ + class Solution { public: static bool isAlienSorted(const std::vector& words, const std::string& order) { diff --git a/cpp/2205/220517.cpp b/cpp/2205/220517.cpp new file mode 100644 index 0000000..2bcecb2 --- /dev/null +++ b/cpp/2205/220517.cpp @@ -0,0 +1,30 @@ +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) {} +}; + +/** + * 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree + * Given two binary trees original and cloned and given a reference to a node target in the original tree. + * The cloned tree is a copy of the original tree. + * Return a reference to the same node in the cloned tree. + * Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree. + */ + +class Solution { +private: + template + static T* notNull(T* p1, T* p2) { + return p1 ? p1 : p2; + } + +public: + static TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) { + if (!original || !cloned || !target) + return nullptr; + return original == target ? cloned : notNull(getTargetCopy(original->left, cloned->left, target), getTargetCopy(original->right, cloned->right, target)); + } +}; \ No newline at end of file diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 070d48d..730f500 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220517-CN.cpp) +ADD_EXECUTABLE(2205 220517-CN.cpp 220517.cpp)