关闭

【LeetCode】101. Symmetric Tree 解题报告

标签: leetcodesymmetricbinarytree镜像对称
6493人阅读 评论(0) 收藏 举报
分类:

转载请注明出处: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~~

1
0
查看评论

LeetCode(101)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: ...
  • feliciafay
  • feliciafay
  • 2014-01-17 06:48
  • 2926

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

【101-Symmetric Tree(对称树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its c...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-07 07:29
  • 3781

101. Symmetric Tree Leetcode Python

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 ...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-30 07:28
  • 1488

101. Symmetric Tree [easy] (Python)

题目链接https://leetcode.com/problems/symmetric-tree/题目原文 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)....
  • coder_orz
  • coder_orz
  • 2016-06-03 15:53
  • 1555

LeetCode 101. Symmetric Tree(对称的树)

原题网址:https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F...
  • jmspan
  • jmspan
  • 2016-05-23 03:22
  • 381

Leetcode-Symmetric Tree——判断二叉树是否对称

题目 Symmetric Tree  Total Accepted: 12814 Total Submissions: 40358My Submissions Given a binary tree, check whether it is a...
  • disappearedgod
  • disappearedgod
  • 2014-04-20 01:00
  • 2116

Symmetric Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/symmetric-tree/  这道题是树的题目,本质上还是树的遍历。这里无所谓哪种遍历方式,只需要对相应结点进行比较即可。一颗树对称其实就是看左右子树是否对称,一句话就是左同右,右同左,结点是对称的相...
  • linhuanmars
  • linhuanmars
  • 2014-04-07 04:52
  • 8508

[LeetCode]Symmetric Tree@python

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symme...
  • weixin_38111819
  • weixin_38111819
  • 2018-01-30 17:53
  • 5

【LeetCode with Python】 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 ...
  • nerv3x3
  • nerv3x3
  • 2014-07-06 15:34
  • 5107

【leetcode c++】101 Symmetric Tree

Symmetric Tree Given a binary tree, check whether it is amirror of itself (ie, symmetric around its center). For example, this binary tree is symmet...
  • hqq39
  • hqq39
  • 2015-07-27 14:39
  • 324
    个人资料
    • 访问:1293097次
    • 积分:9493
    • 等级:
    • 排名:第2256名
    • 原创:182篇
    • 转载:9篇
    • 译文:2篇
    • 评论:797条
    联系方式
    博客专栏
    最新评论
    CNZZ统计