给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
说明:
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
递归写法:
/**
* 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) {
if(root==nullptr) return true;
return isSymmetric(root->left,root->right);
}
bool isSymmetric(TreeNode *p,TreeNode *q){
if(p==nullptr&&q==nullptr) return true;
if(p==nullptr||q==nullptr) return false;
return p->val==q->val&&isSymmetric(p->left,q->right)&&isSymmetric(p->right,q->left);
}
};
迭代写法:
/**
* 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) {
if(!root) return true;
stack<TreeNode*> s;
s.push(root->left);
s.push(root->right);
while(!s.empty()){
auto p = s.top();
s.pop();
auto q = s.top();
s.pop();
if(!q&&!p) continue;
if(!p||!q) return false;
if(p->val!=q->val) return false;
s.push(p->left);
s.push(q->right);
s.push(p->right);
s.push(q->left);
}
return true;
}
};