简单题,下班水的,方法不优雅,博客里LeetCode107改改就好了
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root is None:
return []
else:
result = []
que = [root]
num = 1
while num != 0:
tmp = []
new_num = num
num = 0
for i in range(new_num):
node = que.pop(0)
tmp.append(node.val)
if node.left:
que.append(node.left)
num += 1
if node.right:
que.append(node.right)
num += 1
result.append(tmp)
return result