Login light

leetcode/103. 二叉树的锯齿形层次遍历.md

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
        retList = []
        self.getNode(0, root, retList)
        for index in range(len(retList)):
            if index % 2 == 1:
                retList[index].reverse()
        return retList
    
    def getNode(self, index, child: TreeNode, retList):
        if None == child:
            return
        if len(retList) < index + 1:
            retList.append([child.val])
        else:
            retList[index].append(child.val)
        self.getNode(index + 1, child.left, retList)
        self.getNode(index + 1, child.right, retList)