LeetCode题解:Symmetric Tree(有4种解法)

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);
    }
} 







1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值