feat: 101_symmetric_tree
This commit is contained in:
parent
ae8598a2b1
commit
7e7e210169
26
101_symmetric_tree/main.cpp
Normal file
26
101_symmetric_tree/main.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode *left;
|
||||||
|
TreeNode *right;
|
||||||
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isSymmetric(TreeNode *root) {
|
||||||
|
if (root == nullptr) return true;
|
||||||
|
return isSymmetric(root->left, root->right);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isSymmetric(TreeNode *left, TreeNode *right) {
|
||||||
|
if (!left && !right) return true;
|
||||||
|
if (!left || !right) return false;
|
||||||
|
if (left->val != right->val) return false;
|
||||||
|
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user