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)
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.
@jay Yes, you are correct. I was just used to write this, and it is truly not a good habit. thanks!