剑指Offer:对称的二叉树【28】

剑指Offer:对称的二叉树【28】

题目描述

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

题目分析

Java题解

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
   boolean isSymmetrical(TreeNode pRoot)
    {
        if(pRoot==null)
            return true;

        return isSymmetricalCore(pRoot.left,pRoot.right);
    }

    boolean isSymmetricalCore(TreeNode pRoot1,TreeNode pRoot2)
    {
        if(pRoot1==null&&pRoot2==null)
            return true;
        if(pRoot1==null||pRoot2==null)
            return false;
        if(pRoot1.val!=pRoot2.val)
            return false;
        return isSymmetricalCore(pRoot1.left,pRoot2.right)&&isSymmetricalCore(pRoot1.right,pRoot2.left);
    }
}

  

猜你喜欢

转载自www.cnblogs.com/MrSaver/p/9245486.html