https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/
解题思路 :
二叉树层序便利后, 偶数层子节点倒序即可.(第一层为root节点)
class Solution(object):
def zigzagLevelOrder(self, root):
self.n = 1
self.queue = []
self.result = []
if root != None and root.left == None and root.right == None:
tmp = [root.val]
self.result.append(tmp)
return self.result
self.queue.append(root)
while( len(self.queue) > 0 ):
tmp = []
tmp_list = []
tmp_len = len(self.queue)
for i in range(0, tmp_len):
tmp_node = self.queue[0]
if tmp_node != None:
tmp.append(tmp_node.val)
tmp_list.append(tmp_node.left)
tmp_list.append(tmp_node.right)
del self.queue[0]
self.queue = tmp_list
self.n += 1
if len(tmp) == 0:
continue
self.result.append(tmp)
for i in range(0, len(self.result)):
if i % 2 == 1:
self.result[i] = list(reversed(self.result[i]))
return self.result