算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
python 代码
Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def printFromTopToBottom(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
res=[]
tmp=[root]
while tmp:
size=len(tmp)
row=[]
for i in tmp:
row.append(i.val)
res.extend(row)
for i in range(size):
node=tmp.pop(0)
if node.left:
tmp.append(node.left)
if node.right:
tmp.append(node.right)
return res