Skip to content

Commit 06fdad2

Browse files
committed
2020-01-21
1 parent 67b55f2 commit 06fdad2

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

0102.二叉树的层次遍历/0102-二叉树的层次遍历.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ def levelOrder(self, root):
1111
:type root: TreeNode
1212
:rtype: List[List[int]]
1313
"""
14-
queue = [root]
14+
if not root:
15+
return []
16+
from collections import deque
17+
queue = deque([root])
1518
res = []
1619
while queue:
17-
next_queue = []
1820
layer = []
19-
for node in queue:
20-
if node:
21-
layer.append(node.val)
22-
next_queue += [node.left, node.right]
23-
queue = next_queue[:]
24-
if layer:
25-
res.append(layer[:])
26-
return res
27-
28-
21+
for _ in range(len(queue)):
22+
cur = queue.popleft()
23+
if cur:
24+
layer.append(cur.val)
25+
queue += [cur.left, cur.right]
26+
res.append(layer)
27+
return res[:-1]
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def flipEquiv(self, root1, root2):
10+
"""
11+
:type root1: TreeNode
12+
:type root2: TreeNode
13+
:rtype: bool
14+
"""
15+
if not root1:
16+
return root2 is None
17+
if not root2:
18+
return root1 is None
19+
if root1.val != root2.val:
20+
return False
21+
return (self.flipEquiv(root1.left, root2.left) and self.flipEquiv(root1.right, root2.right)) or (self.flipEquiv(root1.left, root2.right) and self.flipEquiv(root1.right, root2.left))

0 commit comments

Comments
 (0)