Beats 96.94% with step by step explanation,
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nThis solution uses a level-order traversal of the binary tree and a queue
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution uses a level-order traversal of the binary tree and a queue to keep track of the nodes. For each level, it sets the next pointers of the nodes to the next node in the queue, except for the last node in the level. It then adds the node\'s children to the queue if they exist, and continues with the next level. The function returns the root node of the binary tree.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def connect(self, root: \'Node\') -> \'Node\':\n        if not root:\n            return root\n        \n        # Start with the root node\n        queue = [root]\n        \n        while queue:\n            # Get the number of nodes in the current level\n            size = len(queue)\n            \n            # Traverse through the nodes in the current level\n            for i in range(size):\n                # Get the first node from the queue\n                node = queue.pop(0)\n                \n                # If it\'s not the last node in the level, set its next to the next node in the queue\n                if i < size - 1:\n                    node.next = queue[0]\n                \n                # Add the node\'s children to the queue if they exist\n                if node.left:\n                    queue.append(node.left)\n                if node.right:\n                    queue.append(node.right)\n        \n        # Return the root node\n        return root\n\n```
['Tree', 'Depth-First Search', 'Breadth-First Search', 'Python', 'Python3']