-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100.same_tree.cpp
44 lines (32 loc) · 893 Bytes
/
100.same_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "./include/header/tree.hpp"
using namespace std;
class Solution
{
public:
bool isSameTree(TreeNode *p, TreeNode *q)
{
if (!p && !q)
{
return true;
}
if (p && q && p->val == q->val)
{
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
return false;
}
};
int main()
{
Solution s;
vector<int> p1 = {1, 2, 3};
vector<int> q1 = {1, 2, 3};
assert(s.isSameTree(TreeNode::deserialize(p1), TreeNode::deserialize(q1)) == true);
vector<int> p2 = {1, 2};
vector<int> q2 = {1, INT_MAX, 2};
assert(s.isSameTree(TreeNode::deserialize(p2), TreeNode::deserialize(q2)) == false);
vector<int> p3 = {1, 2, 1};
vector<int> q3 = {1, 1, 2};
assert(s.isSameTree(TreeNode::deserialize(p3), TreeNode::deserialize(q3)) == false);
return 0;
}