-
Notifications
You must be signed in to change notification settings - Fork 0
/
101-iterative.cpp
55 lines (52 loc) · 1.56 KB
/
101-iterative.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//if a tree is symmetric tree then all elements in one level will be palindrome
class Solution {
public:
bool isSymmetric(TreeNode* root) {
vector<TreeNode*> nodeQueue;
vector<TreeNode*> nextLevel;
vector<TreeNode*> nodeList;
nodeQueue.push_back(root);
while(!nodeQueue.empty()){
for(int i=0;i<nodeQueue.size();i++){
nodeList.push_back(nodeQueue[i]);
if(nodeQueue[i]!=NULL){
nextLevel.push_back(nodeQueue[i]->left);
nextLevel.push_back(nodeQueue[i]->right);
}
}
if(isPalindrome(nodeList)){
nodeList.clear();
nodeQueue = nextLevel;
nextLevel.clear();
}else{
return false;
}
}
return true;
}
bool isPalindrome(vector<TreeNode*> nodeList){
if(nodeList.size()==0) return true;
int start = 0;
int end = nodeList.size()-1;
while(start<end){
if(!isEqual(nodeList[start], nodeList[end])) return false;
start++;
end--;
}
return true;
}
bool isEqual(TreeNode* t1, TreeNode *t2){
if(t1==NULL && t2==NULL) return true;
if(t1==NULL || t2==NULL) return false;
return t1->val == t2->val;
}
};