"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: A Tree
@return: Level order a list of lists of integer
"""
def levelOrder(self, root):
# write your code here
res = []
if root is None:
return res
node = []
node.append(root)
while len(node) is not 0:
row = []
temp = []
for i in range(len(node)):
if node[0].left:
row.append(node[0].left)
if node[0].right:
row.append(node[0].right)
temp.append(node.pop(0).val)
res += [temp]
node = row
return res
Python, LintCode, 69. 二叉树的层次遍历
最新推荐文章于 2018-10-12 15:48:20 发布