two stack python solution


  • 0
    C
    class Solution(object):
        def zigzagLevelOrder(self, root):
            res = []
            if not root:
                return res
            stackE = []
            stackO = []
            stackE.append(root)
            while bool(stackE) ^ bool(stackO):
                sub = []
                if stackE:
                    while stackE:
                        temp = stackE.pop()
                        if temp.left:
                            stackO.append(temp.left)
                        if temp.right:
                            stackO.append(temp.right)
                        sub.append(temp.val)
                else:
                    while stackO:
                        temp = stackO.pop()
                        if temp.right:
                            stackE.append(temp.right)
                        if temp.left:
                            stackE.append(temp.left)
                        sub.append(temp.val)
                res.append(sub)
            return res
    

Log in to reply
 

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