Skip to content

Latest commit

 

History

History
36 lines (28 loc) · 980 Bytes

116._populating_next_right_pointers_in_each_node.md

File metadata and controls

36 lines (28 loc) · 980 Bytes

###116. Populating Next Right Pointers in Each Node

题目:

https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

难度:

Medium

思路: 跟level print一样,把每个level由左到右记录。 然后这层level的右指向右,最后一个指向None。 处理一下边界条件,完工。

看题目tag给的DFS,所以是否有哪种push & pop stack的方法可以特别来处理指向呢-》 有待研究

class Solution(object):
    def connect(self, root):
        """
        :type root: TreeLinkNode
        :rtype: nothing
        """
        if root == None: return root
        thislevel = [root]
        while thislevel:
        	nextlevel = list()
        	for n in thislevel:
        		if n.left: nextlevel.append(n.left)
        		if n.right: nextlevel.append(n.right)
        	for i in range(len(thislevel)-1):
        		thislevel[i].next = thislevel[i+1]
        	thislevel[-1].next = None
        	thislevel = nextlevel