码迷,mamicode.com
首页 > 其他好文 > 详细

102 Binary Tree Level Order Traversal

时间:2015-07-23 06:41:43      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

102 Binary Tree Level Order Traversal 

class Solution:
    # @param {TreeNode} root
    # @return {integer[][]}
    def levelOrder(self, root):
        if root == None:
            return []
        ans, q, level = [], [root, None], []
        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:
                ans.append(level)
                level = []
                if q == []:
                    break
                else:
                    q.append(None)
        return ans

 

102 Binary Tree Level Order Traversal

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4669148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!