递归与二叉树_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)
posted @ 2019-03-17 13:56  AceKo  阅读(157)  评论(0编辑  收藏  举报