103. Binary Tree Zigzag Level Order Traversal
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
Python
DFS
from collections import deque
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
result = []
def dfs(node, level):
if level >= len(result):
result.append(deque([node.val]))
else:
if level % 2 == 0:
result[level].append(node.val)
else:
result[level].appendleft(node.val)
if node.left:
dfs(node.left, level+1)
if node.right:
dfs(node.right, level+1)
dfs(root, 0)
return result
BFS
(TODO)