zoukankan      html  css  js  c++  java
  • LeetCode小白菜笔记[23]:Symmetric Tree

    LeetCode小白菜笔记[23]:Symmetric Tree

    101. Symmetric Tree [easy]

    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 symmetric:

        1
       / 
      2   2
     /  / 
    3  4 4  3

    But the following [1,2,2,null,3,null,3] is not:

        1
       / 
      2   2
          
       3    3

    Note:
    Bonus points if you could solve it both recursively and iteratively.

    要求是检查一棵树是不是对称,就是结构上对称,并且相对应的位置值相同。然后题目还特别提及可以用递归也可以用循环。首先,递归的方法的基本思路是维护两棵树,分别以左边和右边的对应位置为根节点,首先检查这两个根节点是否相同,然后检查左边为根的左孩子和右边的右孩子以及左边的右孩子和右边的左孩子是否相等,如果根相等且左右孩子分别对称,那么整颗子树就对称。这样就构造了一个递归。code如下:

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def isSymmetric(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            def SymSub(subl,subr):
                if not subl and not subr:
                    return True
                elif not subl or not subr:
                    return False
                else:
                    if subl.val == subr.val and SymSub(subl.left, subr.right) and SymSub(subl.right, subr.left):
                        return True
                    else:
                        return False
            return SymSub(root,root)

    如果用循环的方法,code如下:

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def isSymmetric(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            qu = [root,root]
            while not len(qu) == 0:
                subl = qu[0]
                subr = qu[1]
                qu = qu[2:]
                if not subl and not subr:
                    continue
                elif not subl or not subr:
                    return False
                elif not subl.val == subr.val:
                    return False
                else:
                    qu.extend([subl.left, subr.right, subl.right, subr.left])
            return True

    上面的这种iterative相当于是构造了一个队列queue,即qu,每次将对应的应当相等的位置的节点成对的加入队列,并每一对进行比较,并且如果比较结果相等则在加上它们下面的对应项。基本可以看做是recursive版本的一个iteration的实现。如果有一个不相等或者不对称的地方直接返回false,相等则继续比较,最终如果都比较完成,即queue空了且没返回false,说明对称的位置都是相等的。因此返回true。

    2018年2月13日22:08:21

  • 相关阅读:
    使用SWFUpload无刷新上传图片
    WP多语言
    C#将DataTable转化为List<T>
    Android 多语言
    Android开发 Failed to install *.apk on device 'emulator-5554': EOF
    ASP.NET MVC中将数据从Controller传递到视图
    安装和卸载程序时总是出现2502,2503错误代码的解决方法
    position固定菜单栏
    [position]返回顶部
    [css]后台管理系统布局
  • 原文地址:https://www.cnblogs.com/morikokyuro/p/13256806.html
Copyright © 2011-2022 走看看