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
题解:
判断一棵二叉树是否是自身的镜像,即:一棵爱美的二叉树去照镜子,发现镜子里的自己竟然和自己长得一样诶!!!
文字看不懂的话,就看看题目给的Example咯 -..-
解题思路:
这道题其实有很多的思路,我都列出来吧:
1、我看到这道题的想法是这样的:一棵有左右孩子,而且非空的二叉树,对它的左孩子进行层次遍历,并把结果存起来;对它的右孩子进行逆序的层次遍历(每一层从右到左遍历),并把结果存起来。最后把左右孩子进行对比。显然这个方法是比较笨的 -..-
2、把所有左子树和“镜像”对应的右子树进行对比,不断的递归。如果每一次对比返回的结果都是true,则是镜像二叉树。
3、中序遍历,思路和1、差不多
4、分别遍历根节点的左右子树,获得一个字符串。然后对比。
代码:
这里的代码呢我都是以LeetCode上先发出来的大神的代码为主,主要是考虑到LeetCode上有人会因为代码思想相似给人Vote Down,所以我也本着不鼓励重复发明轮子的思想,在这贴上首发的大神们的代码。反正都是学习嘛,都OK啦。
1、by:lucastan
#include<queue>
using namespace std;
typedef pair<TreeNode*,TreeNode*> nodepair;
class Solution {
public:
bool isSymmetricRecursive(TreeNode*a,TreeNode*b){
if(a){
return b && a->val==b->val &&
isSymmetricRecursive(a->left,b->right) &&
isSymmetricRecursive(a->right,b->left);
}
return !b;
}
bool isSymmetricRecursive(TreeNode*root){
return !root || isSymmetricRecursive(root->left,root->right);
}
bool isSymmetric(TreeNode *root) {
// Level-order BFS.
queue<nodepair> q;
if(root)
q.push(make_pair(root->left,root->right));
while(q.size()){
nodepair p=q.front(); q.pop();
if(p.first){
if(!p.second)return false;
if(p.first->val != p.second->val) return false;
// the order of children pushed to q is the key to the solution.
q.push(make_pair(p.first->left,p.second->right));
q.push(make_pair(p.first->right,p.second->left));
}
else if(p.second) return false;
}
return true;
}
};
2、by:lzklee
public boolean checkSymmetric(TreeNode lsubTree,TreeNode rsubTree){
if(lsubTree==null&&rsubTree==null) return true;
else if(lsubTree!=null&&rsubTree==null) return false;
else if(lsubTree==null&&rsubTree!=null) return false;
else if(lsubTree.val!=rsubTree.val) return false;
boolean lt=checkSymmetric(lsubTree.left,rsubTree.right);
boolean rt=checkSymmetric(lsubTree.right,rsubTree.left);
return lt&&rt;
}
public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
return checkSymmetric(root.left,root.right);
} public boolean checkSymmetric(TreeNode lsubTree,TreeNode rsubTree){
if(lsubTree==null&&rsubTree==null) return true;
else if(lsubTree!=null&&rsubTree==null) return false;
else if(lsubTree==null&&rsubTree!=null) return false;
else if(lsubTree.val!=rsubTree.val) return false;
boolean lt=checkSymmetric(lsubTree.left,rsubTree.right);
boolean rt=checkSymmetric(lsubTree.right,rsubTree.left);
return lt&&rt;
}
public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
return checkSymmetric(root.left,root.right);
}
3、
by:
walnutown
<span style="font-size:12px;">public class Solution {
ArrayList<String> nodeArr;
public boolean isSymmetric(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if (root == null){
return true;
}
nodeArr = new ArrayList<String>();
inorderTraversal(root);
// compare in-order traversal list
int i = 0;
int j = nodeArr.size() -1;
while (i < j){
if (!nodeArr.get(i).equals(nodeArr.get(j))){
return false;
}
i++;
j--;
}
return true;
}
public void inorderTraversal(TreeNode n){
// if empty node, save as ‘#’ to occupy this place
if (n == null){
nodeArr.add("#");
return;
}
if (n.left == null && n.right == null) {
nodeArr.add(""+n.val);
} else {
inorderTraversal(n.left);
nodeArr.add(""+n.val);
inorderTraversal(n.right);
}
}
}</span>
4、by:
cow12331
public boolean isSymmetric(TreeNode root) {
return left(root).equals(right(root));
}
public static String left(TreeNode root) {
if(root == null) return "#";
else {
return root.val + left(root.left) + left(root.right);
}
}
public static String right(TreeNode root) {
if(root == null) return "#";
else {
return root.val + right(root.right) + right(root.left);
}
}