7 lines, iterative, real O(1) space
Simply do it level by level, using the next-pointers of the current level to go through the current level and set the next-pointers of the next level.\n\nI say 
Simply do it level by level, using the `next`-pointers of the current level to go through the current level and set the `next`-pointers of the next level.\n\nI say "real" O(1) space because of the many recursive solutions ignoring that recursion management needs space.\n\n    def connect(self, root):\n        while root and root.left:\n            next = root.left\n            while root:\n                root.left.next = root.right\n                root.right.next = root.next and root.next.left\n                root = root.next\n            root = next
['Python']