Short python code


  • 0
    A
    class Solution(object):
        def zigzagLevelOrder(self, root):
            if root is None: return []
            q = [[root]]
            for i, level in enumerate(q):
                record = []
                for j in range(len(level)-1, -1, -1):
                    node = level[j]
                    if i%2 == 0:
                        if node.right: record.append(node.right)
                        if node.left: record.append(node.left)
                    else:
                        if node.left: record.append(node.left)
                        if node.right: record.append(node.right)
                if record: q.append(record)
            return [[x.val for x in rec] for rec in q]

Log in to reply
 

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