判断一个二叉树是否是轴对称的是一个经典的算法问题,下面结合leetcode上的Symmetric Tree给出判断对称树的两种方法。
先看看问题描述:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3
树的结构:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
方法一:
层次遍历是最直观的方法。对数进行层次遍历,记录每一层的节点,然后对每一层的value组成的字符串判断是不是对称串。算法的时间复杂度为O(nlgn),非最优,侥幸AC。
class Solution {
public:
bool isSymmetric(TreeNode *root) {
if(!root)
return true;
if(root->left==NULL && root->right!=NULL || root->left!=NULL && root->right==NULL)
return false;
if(!root->left && !root->right)
return true;
mm.insert(make_pair(root->left,root->right));
return judge();
}
private:
multimap<TreeNode*,TreeNode*> mm; //存放每层的节点,将对称位置上的一对节点存在一个key-value对里面
bool judge(){
if(mm.empty())
return true;
multimap<TreeNode*,TreeNode*> tmp(mm);
mm.clear();
for(multimap<TreeNode*,TreeNode*>::iterator it=tmp.begin();it!=tmp.end();++it){
if(it->first->val!=it->second->val)
return false;
if(it->first->left && !it->second->right)
return false;
if(!it->first->left && it->second->right)
return false;
if(it->first->right && !it->second->left)
return false;
if(!it->first->right && it->second->left)
return false;
if(it->first->right && it->second->left)
mm.insert(make_pair(it->first->right,it->second->left));
if(it->first->left && it->second->right)
mm.insert(make_pair(it->first->left,it->second->right));
}
return judge(); //递归到树的下一层
}
};
方法二:
不采用层次遍历。直接比较对称位置:left的right和right的left比较,left的left和right的right比较。时间复杂度O(n)下面给出递归和非递归两个版本:
1、递归版本
class Solution {
public:
bool isSymmetric(TreeNode *root) {
return root ? isSymmetric(root->left, root->right) : true;
}
bool isSymmetric(TreeNode *left, TreeNode *right) {
if (!left && !right) return true;
if (!left || !right) return false;
return left->val == right->val && isSymmetric(left->left, right->right)&& isSymmetric(left->right, right->left);
}
};
2、非递归版本
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 (!p && !q) 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;
}
};