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

leetcode-mid-Linked list- 116. Populating Next Right Pointers in Each Node

时间:2019-06-03 17:28:22      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:tco   def   class   lin   etc   list   int   color   bsp   

mycode   93.97%

"""
# Definition for a Node.
class Node(object):
    def __init__(self, val, left, right, next):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
"""
class Solution(object):
    def connect(self, root):
        if not root:
            return None
        
        if root and root.left:
            root.left.next = root.right
            if root.next != None:
                #print(root.val,root.next)
                root.right.next = root.next.left
            self.connect(root.left)
            self.connect(root.right)
        return root

参考:

其实第二个if可以只写root.left,这样阔以快一丢丢啦

class Solution(object):
    def connect(self, root):
        """
        :type root: Node
        :rtype: Node
        """
        if not root:
            return None
        if root.left:
            root.left.next = root.right
            if root.next:
                root.right.next = root.next.left
            self.connect(root.left)
            self.connect(root.right)
        return root

 

leetcode-mid-Linked list- 116. Populating Next Right Pointers in Each Node

标签:tco   def   class   lin   etc   list   int   color   bsp   

原文地址:https://www.cnblogs.com/rosyYY/p/10968584.html

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