zoukankan      html  css  js  c++  java
  • 103. 二叉树的锯齿形层次遍历




    # 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 not root:
                return []
            level_queue = []
            ans = []
            level_queue.append(root)
            while level_queue:
                num = len(level_queue)
                temp = []
                while num > 0:
                    cur = level_queue.pop(0)
                    temp.append(cur.val)
                    if cur.left:
                        level_queue.append(cur.left)
                    if cur.right:
                        level_queue.append(cur.right)
                    num -= 1
                ans.append(temp)
            for i in range(ans):
                if i % 2 != 0:
                    ans[i] = ans[i][::-1]
            return ans
    

  • 相关阅读:
    wc
    1.11考试
    diff
    C++11新利器
    vimdiff
    [学习笔记]多项式
    rev
    [AH2017/HNOI2017]礼物
    tr
    bzoj2555: SubString
  • 原文地址:https://www.cnblogs.com/panweiwei/p/13585602.html
Copyright © 2011-2022 走看看