-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution_recursive.cc
More file actions
56 lines (47 loc) · 1.28 KB
/
Copy pathsolution_recursive.cc
File metadata and controls
56 lines (47 loc) · 1.28 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
/*************************************************************************
> File Name: solution_plain.cc
> Author: MarkWoo
> Mail:wcgwuxinwei@gmail.com
> Created Time: 2015年09月07日 星期一 00时45分42秒
************************************************************************/
/*
* Given two binary trees, write a function to check if they are equal or not.
* Two binary trees are considered equal if they are structurally identical and
* the nodes have the same value.
*/
#include<iostream>
#include<string>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x): val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode *q) {
bool is_same_label = false;
if (!p && !q) {
is_same_label = true;
} else if (p && q){
if (p->val == q->val) {
is_same_label = true;
if (p->left && q->left) {
is_same_label = isSameTree(p->left, q->left);
} else if (p->left || q->left) {
is_same_label = false;
}
if (p->right && q->right && is_same_label) {
is_same_label = isSameTree(p->right, q->right);
} else if (p->right || q->right) {
is_same_label = false;
}
}
}
return is_same_label;
}
};
int main(void) {
return 0;
}