#102
Binary Tree Level Order Traversal
medium· Treesruns: 0Given the root of a binary tree, return the level order traversal of its nodes' values. Return a list of lists where each inner list contains the values at one level, from left to right.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 575
from collections import deque
class Solution:
def levelOrder(self, root: TreeNode | None) -> list[list[int]]:
if not root:
return []
result = []
queue = deque([root])
while queue:
level = []
for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(level)
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.