简单题,方法也不优雅,LeetCode107改改就行了,锯齿形就是掌握好层和节点左右顺序就好
class Solution(object):
def levelOrderBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root is None:
return []
else:
result = []
que = [root]
num = 1
floor = 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
if floor % 2 == 0:
result.insert(0, tmp[::-1])
else:
result.insert(0, tmp)
floor += 1
return result