-
Notifications
You must be signed in to change notification settings - Fork 0
/
No101.cpp
57 lines (53 loc) · 1008 Bytes
/
No101.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
/**
* 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 {
public:
bool isSymmetric(TreeNode* root) {
preOrderForward(root);
preOrderBackward(root);
if(seq1==seq2)
{
return true;
}
else
{
return false;
}
}
private:
vector<int> seq1;
vector<int> seq2;
void preOrderForward(TreeNode* root)
{
if(root)
{
if(!root->left && root->right)
{
root->left = new TreeNode(INT_MAX);
}
else if(root->left && !root->right)
{
root->right = new TreeNode(INT_MAX);
}
seq1.push_back(root->val);
preOrderForward(root->left);
preOrderForward(root->right);
}
}
void preOrderBackward(TreeNode* root)
{
if(root)
{
seq2.push_back(root->val);
preOrderBackward(root->right);
preOrderBackward(root->left);
}
}
};