https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
跟102题几乎一样,只不过加个反转。
class Solution:
def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
if root == None:
return []
ans = []
curVal = []
curQueue = [root]
nextQueue = []
zig = 0
while curQueue:
top = curQueue.pop(0)
curVal.append(top.val)
if top.left: nextQueue.append(top.left)
if top.right: nextQueue.append(top.right)
if(curQueue == []):
curQueue = nextQueue
nextQueue = []
if zig:
ans.append(curVal[::-1])
zig = 0
else:
ans.append(curVal)
zig = 1
curVal = []
return ans