add: 230313

This commit is contained in:
Eatswap 2023-03-13 23:33:14 +08:00
parent 56ed525c17
commit bebbcb0aa5
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 30 additions and 1 deletions

29
cpp/2303/230313.cpp Normal file
View File

@ -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);
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2303)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2303 230313-CN.cpp)
ADD_EXECUTABLE(2303 230313.cpp)