Python solution using queue


  • 4
    T

    '''
    class Solution(object):

        if not root:
        	return []
    
        from collections import deque
    
        result = deque()
        queue = deque([root])
        while(queue):
        	level=[]
        	for i in range(len(queue)):
        		front=queue.popleft()
        		level.append(front.val)
        		if front.left:
        			queue.append(front.left)
        		if front.right:
        			queue.append(front.right)
        	result.appendleft(level)
        return list(result)
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.