101. Symmetric Tree

Posted optor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了101. Symmetric Tree相关的知识,希望对你有一定的参考价值。

原题链接:https://leetcode.com/problems/symmetric-tree/description/

我的思路

这道题目大意是判断一颗二叉树是否是对称树,我看题目是“Easy”级别的,以为不难呢!然后我写出来如下实现:

import java.util.Stack;

/**
 * Created by clearbug on 2018/2/26.
 */
public class Solution {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);

        TreeNode rootLeft = new TreeNode(2);
        TreeNode rootRight = new TreeNode(3);
        root.left = rootLeft;
        root.right = rootRight;

        TreeNode leftLeft = new TreeNode(3);
        TreeNode leftRight = null;
        rootLeft.left = leftLeft;
        rootLeft.right = leftRight;

        TreeNode rightLeft = new TreeNode(2);
        TreeNode rightRight = null;
        rootRight.left = rightLeft;
        rootRight.right = rightRight;

        Solution s = new Solution();
        System.out.println(s.isSymmetric(root));
    }

    public boolean isSymmetric(TreeNode root) {
        Stack<Integer> valStack = new Stack<>();
        inOrderTraverse(root, valStack);
        if (valStack.size() == 1) {
            return true;
        }

        Integer[] valArr = new Integer[valStack.size()];
        valStack.toArray(valArr);
        int index = valArr.length / 2;
        while (index >= 0) {
            if (valArr[index] == null) {
                if (valArr[valArr.length - 1 - index] != null) {
                    return false;
                }
            } else if (!valArr[index].equals(valArr[valArr.length - 1 - index])) {
                return false;
            }
            index--;
        }
        return true;
    }

    public void inOrderTraverse(TreeNode node, Stack<Integer> stack) {
        if (node == null) {
            stack.push(null);
            return;
        }

        if (node.left == null && node.right == null) {
            stack.push(node.val);
            return;
        }

        inOrderTraverse(node.left, stack);
        stack.push(node.val);
        inOrderTraverse(node.right, stack);
    }
}

大致想法就是利用中序遍历,然后将遍历的节点值放入栈中,最后将栈转化为一个一维数组,然后对比数组里对称位置上的值是否一致!
初看这个思路是没问题,但是总是有些测试用例运行时失败了。。。看来是我想错了,那就去看看官方答案吧!

官方方法一:递归

不得不说官方答案就是高端大气上档次啊!利用了镜子反射原理+递归实现了,代码也是极其简洁。就是镜子反射原理我还是不大能彻底理解啊:

/**
 * Created by clearbug on 2018/2/26.
 */
public class Solution {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);

        TreeNode rootLeft = new TreeNode(2);
        TreeNode rootRight = new TreeNode(3);
        root.left = rootLeft;
        root.right = rootRight;

        TreeNode leftLeft = new TreeNode(3);
        TreeNode leftRight = null;
        rootLeft.left = leftLeft;
        rootLeft.right = leftRight;

        TreeNode rightLeft = new TreeNode(2);
        TreeNode rightRight = null;
        rootRight.left = rightLeft;
        rootRight.right = rightRight;

        Solution s = new Solution();
        System.out.println(s.isSymmetric(root));
    }

    public boolean isSymmetric(TreeNode root) {
        return isMirror(root, root);
    }

    public boolean isMirror(TreeNode t1, TreeNode t2) {
        if (t1 == null && t2 == null) {
            return true;
        }
        if (t1 == null || t2 == null) {
            return false;
        }
        return (t1.val == t2.val) && isMirror(t1.right, t2.left) && isMirror(t1.left, t2.right);
    }

}

官方方法二:迭代

这个方法主要还是利用了镜子反射原理,外加队列以及类似 BFS 的思路来实现的:

public boolean isSymmetric(TreeNode root) {
    Queue<TreeNode> q = new LinkedList<>();
    q.add(root);
    q.add(root);
    while (!q.isEmpty()) {
        TreeNode t1 = q.poll();
        TreeNode t2 = q.poll();
        if (t1 == null && t2 == null) continue;
        if (t1 == null || t2 == null) return false;
        if (t1.val != t2.val) return false;
        q.add(t1.left);
        q.add(t2.right);
        q.add(t1.right);
        q.add(t2.left);
    }
    return true;
}

以上是关于101. Symmetric Tree的主要内容,如果未能解决你的问题,请参考以下文章

101. Symmetric Tree

[LeetCode]题解(python):101-Symmetric Tree

101. Symmetric Tree

101. Symmetric Tree

LeetCode OJ 101Symmetric Tree

101. Symmetric Tree