-
Notifications
You must be signed in to change notification settings - Fork 0
/
101.symmetric-tree.57769205.ac.cpp
90 lines (90 loc) · 1.63 KB
/
101.symmetric-tree.57769205.ac.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* [101] Symmetric Tree
*
* https://leetcode.com/problems/symmetric-tree
*
* Easy (38.38%)
* Total Accepted:
* Total Submissions:
* Testcase Example: '[1,2,2,3,4,4,3]'
*
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric
* around its center).
*
*
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
*
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
*
*
*
* But the following [1,2,2,null,3,null,3] is not:
*
* 1
* / \
* 2 2
* \ \
* 3 3
*
*
*
*
* Note:
* Bonus points if you could solve it both recursively and iteratively.
*
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
vector<int> a1, a2;
void houxu1(TreeNode* root)
{
if (!root) {
a1.push_back(INT_MAX);
return;
}
houxu1(root->left);
houxu1(root->right);
a1.push_back(root->val);
}
void houxu2(TreeNode* root)
{
if (!root) {
a2.push_back(INT_MAX);
return;
}
houxu2(root->right);
houxu2(root->left);
a2.push_back(root->val);
}
public:
bool isSymmetric(TreeNode* root) {
if (!root) {
return true;
}
houxu1(root->left);
houxu2(root->right);
if (a1.size() != a2.size()) {
return false;
} else {
for (int i = 0 ; i < a1.size(); i++) {
//cout << a1[i] << ' ' << a2[i] << endl;
if (a1[i] != a2[i]) {
return false;
}
}
}
return true;
}
};