zoukankan      html  css  js  c++  java
  • 递归与二叉树_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)
  • 相关阅读:
    nodejs安装以及配置
    java第三周学习总结
    java第二周学习总结
    java第一周学习总结
    调查问卷
    2016.2.19 学习总结
    第一周学习总结
    第一周学习总结
    假期马原学习计划
    20145335郝昊《java程序设计》第2次实验报告
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10546763.html
Copyright © 2011-2022 走看看