LeetCode 103. Binary Tree Zigzag Level Order Traversal

Posted 小丑进场

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
        

                
                

  

以上是关于LeetCode 103. Binary Tree Zigzag Level Order Traversal的主要内容,如果未能解决你的问题,请参考以下文章

#Leetcode# 103. Binary Tree Zigzag Level Order Traversal

[leetcode tree]103. Binary Tree Zigzag Level Order Traversal

LeetCode103. Binary Tree Zigzag Level Order Traversal 解题报告

[LeetCode]题解(python):103-Binary Tree Zigzag Level Order Traversal

Leetcode 103. Binary Tree Zigzag Level Order Traversal

Leetcode 103. Binary Tree Zigzag Level Order Traversal