Graph Medium

Level Order

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def levelOrder(self, root: "TreeNode") -> "List[List[int]]":
    levelorder = list()

    current_level = [root]

    while root and current_level:
        next_level = list()
        current_vals = list()

        for node in current_level:
            current_vals.append(node.val)

            if node.left:
                next_level.append(node.left)

            if node.right:
                next_level.append(node.right)

        levelorder.append(current_vals)
        current_level = next_level

    return levelorder

🎰