AcWing 44. 分行从上往下打印二叉树-python
原题链接
中等
作者:
acwing_827
,
2019-09-04 17:41:17
,
所有人可见
,
阅读 1133
# 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 printFromTopToBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
row, res = [], []
if not root: return []
q = [root]
i = 1
while q:
node = q.pop(0)
i-=1
row.append(node.val)
if node.left: q.append(node.left)
if node.right: q.append(node.right)
if i == 0:
res.append(row)
row=[]
i = len(q)
return res