Skip to content

Instantly share code, notes, and snippets.

@uncountablecat
Created September 13, 2018 01:15
Show Gist options
  • Save uncountablecat/2da821f41026b2e0de5016b5402b6baf to your computer and use it in GitHub Desktop.
Save uncountablecat/2da821f41026b2e0de5016b5402b6baf to your computer and use it in GitHub Desktop.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
queue = []
queue.append(root)
while all(node is None for node in queue) == False:
if [i.val if i is not None else None for i in queue] == [i.val if i is not None else None for i in queue][::-1]:
num_to_remove = len(queue)
for j in range(num_to_remove):
if queue[j] is not None:
queue.append(queue[j].left)
queue.append(queue[j].right)
else:
queue.append(None)
#queue.append(None) # 注释掉之后就神奇的不超时了
for k in range(num_to_remove):
queue.pop(0)
else:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment