12 lines of python O(1) space, incredibly easy to understand

  • 0

    A good solution should enforce modularity and separate concerns, from which a reusable pattern could be distilled. In this case, it would be best to handle the logic of append, neighours and traversal separately. One could save a line of code by creating a dummy node, although it's trivial.

      def connect(self, root):
                def append(l,node):
                    if not lst: return node,node
                    if not node: return l
                    return lst,node 
                def neighbours(f):
                    fn=None,None # list, tail
                    while f:
                        fn,f=append(append(fn,f.left),f.right), f.next
                    return fn[0]
                while f: # classic bfs

Log in to reply

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