FranciscoPark / isco

0 stars 0 forks source link

Binary Tree Level Order Traversal #51

Open FranciscoPark opened 3 years ago

FranciscoPark commented 3 years ago
# 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 = right
class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []

        result = []
        que = deque()
        que.append(root)

        while que:
            n = len(que)
            result.append([])
            for _ in range(n):
                current = que.popleft()
                result[-1].append(current.val)
                if current.left:
                    que.append(current.left)
                if current.right:
                    que.append(current.right)
        return result
Shreya111111 commented 2 years ago

@FranciscoPark plz assign this issue