python elegant BFS


  • 0
    R
    class Solution(object):
        def zigzagLevelOrder(self, root):
            if not root:
                return []
            q = [root]
            b = True
            r = []
            while q:
                a = list(map(lambda x: x.val, q))
                if b:
                    a.reverse()
                r.append(a)
                q = [i for items in list(map(lambda x: [x.right, x.left], q)) for i in items if i]
                b = not b
            return r
    

Log in to reply
 

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