103. Binary Tree Zigzag Level Order Traversal

103. Binary Tree Zigzag Level Order Traversal

题目:

https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

难度:

Medium

继续用102 的算法作弊

使用102作弊的题目都需要再努力寻求一下别家解法


class Solution(object):
    def zigzagLevelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        res = []
        if root == None: return []
        
        curLevel = [root]
        levelCount = 0
        while curLevel:
            nextLevel = []
            tmpRes = []
            for node in curLevel:
                tmpRes.append(node.val)
                if node.left: nextLevel.append(node.left)
                if node.right: nextLevel.append(node.right)
            if levelCount % 2 == 0: 
                res.append(tmpRes)
            else : 
                tmpRes.reverse()
                res.append(tmpRes)
            levelCount += 1
            curLevel = nextLevel

        return res

你可能感兴趣的:(103. Binary Tree Zigzag Level Order Traversal)