• LeetCode 103. Binary Tree Zigzag Level Order Traversal


    原题

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

    For example:
    Given binary tree [3,9,20,null,null,15,7],

        3
       / 
      9  20
        /  
       15   7
    

    return its zigzag level order traversal as:

    [
      [3],
      [20,9],
      [15,7]
    ]

    解题方法

    方法一

    • 利用栈来实现,将每一层的节点压入栈中,然后通过迭代遍历出每一层节点中的值并加入答案中,通过取模2判断是否正序或逆序
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    # 利用栈迭代
    class Solution(object):
        def zigzagLevelOrder(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if root == None:
                return []
            stack, ret = [root], []
            dep = 0
            while stack:
                level_value = []
                next_level = []
                for node in stack:
                    level_value.append(node.val)
                    if node.left:
                        next_level.append(node.left)
                    if node.right:
                        next_level.append(node.right)
                if dep % 2 == 0:
                    ret.append(level_value)
                else:
                    ret.append(level_value[::-1])
                stack = next_level
                dep += 1
            return ret
                    
                 
            
    
                    
                    
    

      

    方法二

    • 利用双向队列求解,解题思路跟方法一类似
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
                    
    # 利用双向队列  
    # Your runtime beats 90.20 % of python submissions.
    class Solution(object):
        def zigzagLevelOrder(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if root == None:
                return []
            DQ = collections.deque()
            DQ.append(root)
            ret = []
            flag = True
            while DQ:
                length = len(DQ)
                level_value = []
                for i in xrange(length):
                    node = DQ.popleft()
                    if node.left:
                        DQ.append(node.left)
                    if node.right:
                        DQ.append(node.right)
                    if flag:
                        level_value.append(node.val)
                    else:
                        level_value = [node.val] + level_value
                ret.append(level_value)
                flag = not flag
            return ret
            
    
                    
                    
    

      

  • 相关阅读:
    long类型的数据转化为时间
    取到数组中对应位置的文字,并且转成大写
    无key值的json数组解析
    mongo-2ds索引对超过半球范围的适用性测试
    mongoDB-Cannot change the size of a document in a capped collection:
    springboot
    左中右布局的五种实现方式
    spring boot 常见的配置问题
    移动端H5拍照代码实现及外网部署
    JAVA数据库操作回滚小结
  • 原文地址:https://www.cnblogs.com/LiCheng-/p/6902775.html
Copyright © 2020-2023  润新知