diff --git a/cpp/2303/230313.cpp b/cpp/2303/230313.cpp new file mode 100644 index 0000000..7d5c383 --- /dev/null +++ b/cpp/2303/230313.cpp @@ -0,0 +1,29 @@ +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) {} +}; + +/** + * 101. Symmetric Tree + * + * Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). + */ + +class Solution { +private: + static bool helper(TreeNode* L, TreeNode* R) { + if (!L && !R) + return true; + if (!L || !R) + return false; + return (L->val == R->val) && helper(L->right, R->left) && helper(L->left, R->right); + } + +public: + static bool isSymmetric(TreeNode* r) { + return !r || helper(r->left, r->right); + } +}; diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 519606a..83f4175 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 230313-CN.cpp) +ADD_EXECUTABLE(2303 230313.cpp)