Symmetric Tree——LeetCode

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

 

 题意就是给定一个二叉树,判定它是否是自己的镜像,这道题我一开始想着只求一下中序序列,然后反着求一下中序序列(右、根、左)两个相等即可。WA了之后,我想了下,某些情况还是不对的,一颗二叉树是自己的镜像,意味着基于根对称,那么求出原来的树的中序、后序,再求出镜像的树的中序、后序,比较一下是否相等即可。当然这种方法不是很好,因为要求四次。

Talk is cheap>>

  public boolean isSymmetric(TreeNode root) {

        if (root==null)

            return true;

        return getMidOrderSeq(root).equals(getReMidOrderSeq(root))&&getPostOrderSeq(root).equals(getRePostOrderSeq(root));

    }



    public String getMidOrderSeq(TreeNode node) {

        if (node == null) {

            return "";

        }

        return getMidOrderSeq(node.left) + node.val + getMidOrderSeq(node.right);

    }

    public String getReMidOrderSeq(TreeNode node) {

        if (node == null) {

            return "";

        }

        return getReMidOrderSeq(node.right) + node.val + getReMidOrderSeq(node.left);

    }

    public String getPostOrderSeq(TreeNode node) {

        if (node == null) {

            return "";

        }

        return getPostOrderSeq(node.left) +getPostOrderSeq(node.right)+ node.val ;

    }

    public String getRePostOrderSeq(TreeNode node) {

        if (node == null) {

            return "";

        }

        return getRePostOrderSeq(node.right) +getRePostOrderSeq(node.left)+ node.val ;

    }

第二种方法就是用递归,方法参数为两个TreeNode,如果一个为null,检查另一个是否为null,否则就检查这两个节点值是否相等,并递归检查这两个节点的左右子树。

   public boolean isSymmetric(TreeNode root) {

        if (root == null)

            return true;

        return isSymmetric(root, root);



    }



    public boolean isSymmetric(TreeNode left, TreeNode right) {

        if (left == null || right == null) {

            return left == right;

        }

        return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);

    }

第三种方法就是用栈来代替递归,定义两个栈,一左一右,分别将左节点的左孩子(左栈)、右节点的右孩子(右栈)和左节点的右孩子(左栈)、右节点的左孩子(右栈)入栈,循环不变式是两个栈都非空,然后取出栈顶元素,比较是否相等(都为空或值相等),不等则直接return false;然后再将这两个左右节点的左右孩子入栈。。。

    public boolean isSymmetricIter(TreeNode root) {

            if (root == null || (root.left == null && root.right == null))

                return true;

            Stack<TreeNode> leftStack = new Stack<>();

            Stack<TreeNode> rightStack = new Stack<>();

            leftStack.push(root.left);

            rightStack.push(root.right);

            while (!leftStack.isEmpty() && !rightStack.isEmpty()) {

                TreeNode left = leftStack.pop();

                TreeNode right = rightStack.pop();

                if (left == null && right == null) {

                    continue;

                }

                if (left == null || right == null)

                    return false;

                if (left.val != right.val)

                    return false;

                leftStack.push(left.left);

                rightStack.push(right.right);

                leftStack.push(left.right);

                rightStack.push(right.left);

            }

            return true;

        }

 

你可能感兴趣的:(LeetCode)