Python Two deques BFS


  • 0
    C

    from collections import deque
    class Solution(object):

    def rightSideView(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        q1 = deque([root])
        q2 = deque([])
        ans = []
        while q1:
            cur = q1.popleft()
            if cur.left:
                q2.append(cur.left)
            if cur.right:
                q2.append(cur.right)
            if not q1:
                ans = ans+[cur.val]
                q1,q2=q2,q1
        return ans

Log in to reply
 

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