You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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 = rightclassSolution:
deflevelOrder(self, root: Optional[TreeNode]) ->List[List[int]]:
ifnotroot:
returnrootresult= []
queue=collections.deque()
queue.append(root)
# BFSwhilequeue:
level= []
for_inrange(len(queue)):
# Pop and add the node's value to the level arraynode=queue.popleft()
level.append(node.val)
# Add child nodes to the queueifnode.left:
queue.append(node.left)
ifnode.right:
queue.append(node.right)
# Once all nodes at a level have been visited, add the the result arrayresult.append(level)
returnresult
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: