-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-SameTree.cpp
38 lines (35 loc) · 964 Bytes
/
100-SameTree.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
/*=============================================================================
# FileName: 100-SameTree.cpp
# Desc:
# Author: qsword
# Email: huangjian1993@gmail.com
# HomePage:
# Created: 2015-05-05 08:53:56
# Version: 0.0.1
# LastChange: 2015-05-05 08:56:12
# History:
# 0.0.1 | qsword | init
=============================================================================*/
#include <stdio.h>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
//2ms
bool isSameTree(TreeNode* p, TreeNode* q) {
if (!p && !q) {
return true;
}
if ((p && !q) || (!p && q)) {
return false;
}
if (p->val != q->val) {
return false;
}
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};