mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
Add solution to 'a lot' of problems
This commit is contained in:
21
Easy/same-tree/solution.cpp
Normal file
21
Easy/same-tree/solution.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
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 isSameTree(TreeNode *p, TreeNode *q) {
|
||||
if (p != nullptr && q != nullptr) {
|
||||
return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
|
||||
} else if (p == nullptr ^ q == nullptr) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user