-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymmetric-tree.cpp
More file actions
36 lines (33 loc) · 816 Bytes
/
symmetric-tree.cpp
File metadata and controls
36 lines (33 loc) · 816 Bytes
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
/**
* 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){
stack<TreeNode*> s1,s2;
auto t1=root,t2=root;
while(t1 || !s1.empty()){
while(t1 && t2){
s1.push(t1);
t1=t1->left;
s2.push(t2);
t2=t2->right;
}
if(t1 || t2) return false;
t1=s1.top();
s1.pop();
t2=s2.top();
s2.pop();
if(t1->val!=t2->val) return false;
t1=t1->right;
t2=t2->left;
}
return true;
}
};