【LeetCode】101. Symmetric Tree 解题报告

原创 2016年05月30日 23:06:35

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51541984


Subject

出处:https://leetcode.com/problems/symmetric-tree/


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

Explain

判断一颗二叉树是否 镜像对称


Solution

solution 1

判断是否是镜像对称,重要的就是判断对应位置的两个结点的值是否相等。

方法一使用双端队列来实现。

    /**
     * 双端队列 <br />
     * DFS <br />
     * 3ms <br />
     * beats 6.29% of java submissions
     * 
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }

        Deque<TreeNode> deque = new LinkedList<TreeNode>();
        deque.addFirst(root.left);
        deque.addLast(root.right);

        TreeNode preNode = null;
        TreeNode postNode = null;

        while (!deque.isEmpty()) {
            preNode = deque.pollFirst();
            postNode = deque.pollLast();
            if (preNode == null && postNode == null) {
                continue;
            }
            if (preNode == null || postNode == null) {
                return false;
            }
            if (preNode.val != postNode.val) {
                return false;
            } else {
                deque.addFirst(preNode.right);
                deque.addFirst(preNode.left);

                deque.addLast(postNode.left);
                deque.addLast(postNode.right);
            }
        }
        return true;
    }

每次都是从队头和队尾各poll出结点,然后进行比较。

如果条件满足,将队头结点的右结点和左结点入队头
队尾结点的左结点和右结点入队尾
直到队列为空


solution 2

递归方式

    /**
     * 递归方式 <br />
     * 1ms <br />
     * 
     * @param root
     * @return
     */
    public boolean isSysmmetric2(TreeNode root) {
        if (root == null) {
            return true;
        }

        return checkNodes(root.left, root.right);
    }

    public boolean checkNodes(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null) {
            return true;
        }
        if (node1 == null || node2 == null) {
            return false;
        }
        if (node1.val != node2.val) {
            return false;
        } else {
            return checkNodes(node1.left, node2.right)
                    && checkNodes(node1.right, node2.left);
        }
    }

该方法的效率较高~
1ms


solution 3

考虑到之前有做过一个【Same Tree】 的题目和 【Reverse Binary Tree】 的题目。
所以想到,将当前二叉树“反转”,然后在判断这两个树是否一样即可。

    /**
     * 拷贝一颗二叉树,reverse。或者拷贝的时候直接反转。 <br />
     * 然后在使用Same Tree的方法判断这两个树是否一样。 <br />
     * 1ms
     * 
     * @param root
     * @return
     */
    public boolean isSysmmetric3(TreeNode root) {
        if (root == null) {
            return true;
        }

        TreeNode newRootNode = copyNode(root);

        SameTree sameTree = new SameTree();
        return sameTree.isSameTree(root, newRootNode);
    }

    /**
     * 左右对换结点拷贝二叉树
     * 
     * @param node
     * @return
     */
    private TreeNode copyNode(TreeNode node) {
        if (node == null) {
            return null;
        }
        TreeNode treeNode = new TreeNode(node.val);
        treeNode.left = copyNode(node.right);
        treeNode.right = copyNode(node.left);
        return treeNode;
    }

传送门

【Same Tree】

【Reverse Binary Tree】


bingo~~

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

【101-Symmetric Tree(对称树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree, check whether it...

LeetCode(101)Symmetric Tree

题目如下: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)....

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

100. Same Tree

原文题目: Given two binary trees, write a function to check if they are equal or not. Two binary t...

[Leetcode] 101. Symmetric Tree 解题报告

题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)....

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告[LeetCode]https://leetcode.com/problems/symmetric-tree/Total Accepted: 106639 Tot...

leetcode-101. Symmetric Tree 解题笔记

101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun...

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...

[leetcode] 236. Lowest Common Ancestor of a Binary Tree 解题报告

题目链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/    Given a binary tree...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)