对称的二叉树

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

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 comRoot(pRoot.left, pRoot.right);
    }
    boolean comRoot(TreeNode pLeft, TreeNode pRight)
    {
        if (pLeft == null) return pRight == null;
        if (pRight == null) return false;
        if (pLeft.val != pRight.val) return false;
        return comRoot(pLeft.left, pRight.right) && comRoot(pLeft.right, pRight.left);
    }
}


思路二:递归

采用递归的中序遍历(左中右)及反中序遍历(右中左)

import java.util.ArrayList;

public class Solution {

    ArrayList<Integer> list = new ArrayList<>();
    ArrayList<Integer> listReverse  = new ArrayList<>();

    boolean isSymmetrical(TreeNode pRoot)
    {
        inorder(pRoot);
        inorderReverse(pRoot);
        for (int i = 0; i < list.size(); i++)
            if (list.get(i) != listReverse.get(i))
                return false;
        return true;
    }
    private void inorder(TreeNode pRoot)
    {
        if (pRoot == null) {
            list.add(-1);
            return;
        }
        if (pRoot.left == null && pRoot.right == null)
        {
            list.add(pRoot.val);
            return;
        }
        inorder(pRoot.left);
        list.add(pRoot.val);
        inorder(pRoot.right);
    }
    private void inorderReverse(TreeNode pRoot) {
        if (pRoot == null) {
            listReverse.add(-1);
            return;
        }
        if (pRoot.left == null && pRoot.right == null) {
            listReverse.add(pRoot.val);
            return;
        }
        inorderReverse(pRoot.right);
        listReverse.add(pRoot.val);
        inorderReverse(pRoot.left);
    }
}



思路三:非递归

利用非递归的中序遍历(左中右)及反中序遍历(右中左)

import java.util.LinkedList;
public class Solution {
    boolean isSymmetrical(TreeNode pRoot)
    {
        if (pRoot == null)
            return true;
        if (pRoot.left == null || pRoot.right == null)
        {
            if (pRoot.left == null && pRoot.right == null)
                return true;
            return false;
        }

        TreeNode left = pRoot.left;
        TreeNode right = pRoot.right;

        LinkedList<TreeNode> q1 = new LinkedList<>();
        LinkedList<TreeNode> q2 = new LinkedList<>();

        while ((left != null && right != null) || (!q1.isEmpty() && !q2.isEmpty()))
        {
            while (left != null && right != null)
            {
                if (left.val != right.val)
                    return false;
                q1.add(left);
                q2.add(right);
                left = left.left;
                right = right.right;
            }
            if (left == null && right == null)
            {
                if (!q1.isEmpty() && !q2.isEmpty()) {
                    left = q1.poll();
                    left = left.right;
                    right = q2.poll();
                    right = right.left;
                }
            }
            else return false;
        }
        if (left == null && right == null)
            return true;
        else
            return false;
    }
}


思路四:非递归

思路三的变形

import java.util.LinkedList;

public class Solution {
    boolean isSymmetrical(TreeNode pRoot)
    {
        if (pRoot == null)
            return true;
        if (pRoot.left == null || pRoot.right == null)
        {
            if (pRoot.left == null && pRoot.right == null)
                return true;
            return false;
        }

        TreeNode left = pRoot.left;
        TreeNode right = pRoot.right;

        LinkedList<TreeNode> q1 = new LinkedList<>();
        LinkedList<TreeNode> q2 = new LinkedList<>();

        q1.add(left);
        q2.add(right);
        while (!q1.isEmpty())
        {
            left = q1.poll();
            right = q2.poll();
            if (left == null && right == null) continue;
            if ((left == null && right != null) || (left != null && right == null)) return false;
            if (left.val != right.val) return false;
            q1.add(left.left);
            q1.add(left.right);
            q2.add(right.right);
            q2.add(right.left);
        }
        return true;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值