Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 572 Bytes

readme.md

File metadata and controls

30 lines (23 loc) · 572 Bytes

Level order traversal

18-03-2024

Python Solution

from collections import deque

class Solution:
    #Function to return the level order traversal of a tree.
    def levelOrder(self,root):
        # Code here
        if root is None:
            return []

        q=deque()
        out=[]
        q.append(root)
        while q:
            front = q.popleft()
            out.append(front.data)

            if front.left:
                q.append(front.left)
            if front.right:
                q.append(front.right)

        return out