• 递归与二叉树_leetcode101


    #coding=utf-8
    # Definition for a binary tree node.
    class TreeNode(object):
    def __init__(self, x):
    self.val = x
    self.left = None
    self.right = None


    # 递归
    class Solution1(object):
    def isSymmetric(self, root):
    """
    :type root: TreeNode
    :rtype: bool
    """


    return self.isSame(root,root)

    # coding=utf-8
    # Definition for a binary tree node.
    class TreeNode(object):
    def __init__(self, x):
    self.val = x
    self.left = None
    self.right = None

    # 递归
    class Solution1(object):
    def isSymmetric(self, root):
    """
    :type root: TreeNode
    :rtype: bool
    """

    return self.isSame(root, root)

    def isSame(self, left, right):

    if not left and not right:
    return True
    if not left and right:
    return False
    if left and not right:
    return False
    if left.val == right.val:
    return self.isSame(left.left, right.right) and self.isSame(left.right, right.left)
    else:
    return False

    # 迭代
    class Solution2(object):
    def isSymmetric(self, root):
    """
    :type root: TreeNode
    :rtype: bool
    """
    if not root:
    return True
    else:
    return self.isMirror(root.left, root.right)

    def isMirror(self, left, right):
    queue = []
    queue.append(left)
    queue.append(right)

    # 每次成对的比较
    while queue:

    curLeft = queue.pop(0)
    curRight = queue.pop(0)

    if not curLeft and not curRight:
    continue
    if not curLeft or not curRight or curLeft.val != curRight.val:
    return False

    queue.append(curLeft.left)
    queue.append(curRight.right)
    queue.append(curLeft.right)
    queue.append(curRight.left)

    return True

    def createTree(self):

    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(2)

    root.left.left = TreeNode(3)
    root.left.right = TreeNode(4)
    root.right.left = TreeNode(4)
    root.right.right = TreeNode(3)

    return root

    def printTree(self, root):
    res = []

    if not root:
    return res

    queue = []
    queue.append(root)

    while queue:
    level = []
    for i in range(len(queue)):
    curNode = queue.pop(0)
    level.append(curNode.val)

    if curNode.left:
    queue.append(curNode.left)
    if curNode.right:
    queue.append(curNode.right)

    res.append(level)

    return res

    s = Solution2()

    root = s.createTree()

    # print s.printTree(root)

    print s.isSymmetric(root)
    def isSame(self,left,right):

    if not left and not right:
    return True
    if not left and right:
    return False
    if left and not right:
    return False
    if left.val == right.val:
    return self.isSame(left.left,right.right) and self.isSame(left.right,right.left)
    else:
    return False



    # 迭代
    class Solution2(object):
    def isSymmetric(self, root):
    """
    :type root: TreeNode :rtype: bool """ if not root: return True else: return self.isMirror(root.left,root.right) def isMirror(self,left,right): queue = [] queue.append(left) queue.append(right) # 每次成对的比较 while queue: curLeft = queue.pop(0) curRight = queue.pop(0) if not curLeft and not curRight: continue if not curLeft or not curRight or curLeft.val != curRight.val: return False queue.append(curLeft.left) queue.append(curRight.right) queue.append(curLeft.right) queue.append(curRight.left) return True def createTree(self): root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(2) root.left.left= TreeNode(3) root.left.right = TreeNode(4) root.right.left = TreeNode(4) root.right.right = TreeNode(3) return root def printTree(self,root): res = [] if not root: return res queue = [] queue.append(root) while queue: level = [] for i in range(len(queue)): curNode = queue.pop(0) level.append(curNode.val) if curNode.left: queue.append(curNode.left) if curNode.right: queue.append(curNode.right) res.append(level) return ress = Solution2()root = s.createTree()# print s.printTree(root)print s.isSymmetric(root)
  • 相关阅读:
    iPhone开发教程之retain/copy/assign/setter/getter
    关于block使用的5点注意事项
    Block的引用循环问题 (ARC & non-ARC)
    浅谈iOS中MVVM的架构设计与团队协作
    JS学习笔记(不断更新)
    神经网络介绍
    JAVA WEB WITH IDEA
    百度地图标注多个点
    脑筋急转弯——Google 面试
    决策树分类器
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10546763.html
Copyright © 2020-2023  润新知