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

117 Populating Next Right Pointers in Each Node II

时间:2015-07-23 06:45:54      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

117 Populating Next Right Pointers in Each Node II

就是 Bibary Tree Level order Traverse

class Solution:
    # @param root, a tree link node
    # @return nothing
    def connect(self, root):
        if root == None:
            return
        q, pre = [root, None], None
        while True:
            node = q.pop(0)
            if node != None:
                if pre != None:
                    pre.next = node
                pre = node
                if node.left != None:
                    q.append(node.left)
                if node.right != None:
                    q.append(node.right)
            else:
                if q == []:
                    break
                else:
                    q.append(None)
                    pre = None

 

117 Populating Next Right Pointers in Each Node II

标签:

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

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