# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
if not root: return []
result = []
cur_level_nodes = [root]
flag = True
while cur_level_nodes:
cur_level_vals = []
next_level_nodes = []
for node in cur_level_nodes:
cur_level_vals.append(node.val)
if node.left:
next_level_nodes.append(node.left)
if node.right:
next_level_nodes.append(node.right)
cur_level_nodes = next_level_nodes
if flag == True:
result.append(cur_level_vals)
flag = False
else:
result.append(cur_level_vals[::-1])
flag = True
return result