标签:
103 Binary Tree Zigzag Level Order Traversal
level traversal的变种
class Solution: # @param {TreeNode} root # @return {integer[][]} def zigzagLevelOrder(self, root): if root == None: return [] ans, q, level, flag = [], [root, None], [], 0 while True: node = q.pop(0) if node != None: level.append(node.val) if node.left != None: q.append(node.left) if node.right != None: q.append(node.right) else: if flag: ans.append(list(reversed(level))) else: ans.append(level) level = [] flag ^= 1 if q == []: break else: q.append(None) return ans
103 Binary Tree Zigzag Level Order Traversal
标签:
原文地址:http://www.cnblogs.com/dapanshe/p/4669185.html