class Solution {
public:
bool isSymmetric(TreeNode *root) {
if(root==NULL){
return true;
}
stack<TreeNode *> s1,s2;
TreeNode *cur1,*cur2;
s1.push(root->left),s2.push(root->right);
bool flag=true;
while(!s1.empty()&&!s2.empty()){
cur1=s1.top(),cur2=s2.top();
s1.pop(),s2.pop();
if(cur1==NULL&&cur2==NULL){
continue;
}
if(cur1!=NULL&&cur2!=NULL&&cur1->val==cur2->val){
s1.push(cur1->right),s1.push(cur1->left),s2.push(cur2->left),s2.push(cur2->right);
continue;
}
else{
flag=false;
break;
}
}
return flag;
}
};
LeetCode - 101 Symmetric Tree
最新推荐文章于 2024-05-04 19:42:39 发布