题目描述
blablabla
样例
blablabla
算法1
class Solution(object):
def printFromTopToBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
stack = []
result = []
flag = 0
stack.append(root)
while stack:
tmp = []
for i in range(len(stack)):
if not flag:
cur = stack.pop(0)
tmp.append(cur.val)
if cur.left:
stack.append(cur.left)
if cur.right:
stack.append(cur.right)
else:
cur = stack.pop()
tmp.append(cur.val)
if cur.right:
stack.insert(0, cur.right)
if cur.left:
stack.insert(0, cur.left)
result.append(tmp)
flag = not flag
return result