数据结构:树🌲
时间复杂度:O(n)
空间复杂度:O(n)
代码实现:
class Solution:def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:if not root: return []res = []q = [root]while q:curr = []for _ in range(len(q)):out = q.pop(0)if out.left:q.append(out.left)if out.right:q.append(out.right)curr.append(out.val)res.append(curr)return res