Python Solution easy to understand


  • 2
    P
    class Solution(object):
        def connect(self, root):
            """
            :type root: TreeLinkNode
            :rtype: nothing
            """
            if not root:
                return root
            if root.left and root.right:
                root.left.next = root.right
                if root.next:
                    root.right.next = root.next.left
                else:
                    root.right.next = None
            self.connect(root.left)
            self.connect(root.right)
    

  • 0
    J

    There's no reason to return the root in the base case. Also, the else statement is not needed since all next values are set to None.


  • 0
    P

    @jay Yes, you are correct. I was just used to write this, and it is truly not a good habit. thanks!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.