✅ [C++/Java/Python] | BFS | O(n) Time & O(1) Space
I hope the comments are explicit enough to tell about the iterative BFS approach that is used here.\nTime complexity - O(n)\nSpace complexity - O(1)\n\nC++ \n\n
I hope the comments are explicit enough to tell about the iterative BFS approach that is used here.\nTime complexity - O(n)\nSpace complexity - O(1)\n\nC++ \n\n```\nclass Solution {\npublic:\n    Node* connect(Node* root) {\n        if(!root) return root;\n        Node* current = root;\n        while(current) {\n            // this will always be the first node of any level\n            Node* level1stNode = current;\n            // this runs until we iterate over all the nodes of any level\n            while(current) {\n                // this links the child nodes (L child -> next = R child) of the same parent node\n                if(current->left) {\n                    current->left->next = current->right;\n                }\n                // this checks whether there are more nodes towards right at the same level\n                if(current->right && current->next) {\n                    current->right->next = current->next->left;\n                }\n                // move on the next node of the current level (L -> R)\n                current = current->next;\n            }\n            // move onto the first node of the next level\n            current = level1stNode->left;\n        }\n        return root;\n    }\n};\n```\n\nJava\n\n```\nclass Solution {\n    public Node connect(Node root) {\n        if(root == null) return root;\n        Node current = root;\n        while(current != null) {\n\t\t\t// this will always be the first node of any level\n            Node level1stNode = current;\n\t\t\t// this runs until we iterate over all the nodes of any level\n            while(current != null) {\n\t\t\t\t// this links the child nodes (L child -> next = R child) of the same parent node\n                if(current.left != null) {\n                    current.left.next = current.right;\n                }\n\t\t\t\t// this checks whether there are more nodes towards right at the same level\n                if(current.right != null && current.next != null) {\n                    current.right.next = current.next.left;\n                }\n\t\t\t\t// move on the next node of the current level (L -> R)\n                current = current.next;\n            }\n\t\t\t// move onto the first node of the next level\n            current = level1stNode.left;\n        }\n        return root;\n    }\n}\n```\n\nPython\n\n```\nclass Solution:\n    def connect(self, root: \'Optional[Node]\') -> \'Optional[Node]\':\n        if not root: return root\n        \n        self.current = root\n        \n        while self.current:\n            \n            # this will always be the first node of any level\n            self.level1stNode = self.current\n            \n            # this runs until we iterate over all the nodes of any level\n            while self.current:\n                \n                # this links the child nodes (L child -> next = R child) of the same parent node\n                if self.current.left:\n                    self.current.left.next = self.current.right\n                    \n                # this checks whether there are more nodes towards right at the same level\n                if self.current.right and self.current.next:\n                    self.current.right.next = self.current.next.left\n                    \n                # move on the next node of the current level (L -> R)\n                self.current = self.current.next\n            \n            # move onto the first node of the next level\n            self.current = self.level1stNode.left\n            \n        return root\n```\n\n\uD83D\uDE80 Thanks for reading. \nAn upvote would be appreciated.  ^_^\n
['Breadth-First Search', 'C', 'Iterator', 'Java']