🍀Easy BFS 🔥 | |  Java 🔥| | Python 🔥| | C++ 🔥
* Extra space but clean code\n---\njava []\nclass Solution {\n    public Node connect(Node root) {\n        if(root == null)\n            return root;\n        
# * Extra space but clean code\n---\n```java []\nclass Solution {\n    public Node connect(Node root) {\n        if(root == null)\n            return root;\n        Queue<Node> queue = new LinkedList<>();\n        queue.add(root);\n        while(queue.size() > 0)\n        {\n            Deque<Node> dq = new ArrayDeque<>();\n            int length = queue.size();\n            for(int i = 0;i<length;i++)\n            {\n                Node curr = queue.poll();\n                dq.addLast(curr);\n                if(curr.left!=null)\n                    queue.add(curr.left);\n                if(curr.right!=null)\n                    queue.add(curr.right);\n            }\n            while(dq.size() > 1)\n            {\n                Node popped = dq.removeFirst();\n                popped.next = dq.getFirst();\n            }\n            Node popped = dq.removeFirst();\n            popped.next = null;\n        }\n        return root;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    Node* connect(Node* root) {\n        if(root == nullptr)\n            return root;\n        queue<Node*> queue;\n        queue.push(root);\n        while(queue.size() > 0)\n        {\n            deque<Node*> dq;\n            int length = queue.size();\n            for(int i = 0;i<length;i++)\n            {\n                Node* curr = queue.front();\n                queue.pop();\n                dq.push_back(curr);\n                if(curr->left!=nullptr)\n                    queue.push(curr->left);\n                if(curr->right!=nullptr)\n                    queue.push(curr->right);\n            }\n            while(dq.size() > 1)\n            {\n                Node* popped = dq.front();\n                dq.pop_front();\n                popped->next = dq.front();\n            }\n            Node* popped = dq.front();\n            dq.pop_front();\n            popped->next = nullptr;\n        }\n        return root;\n    }\n};\n```\n```python []\nclass Solution:\n    def connect(self, root: \'Node\') -> \'Node\':\n        if not root:\n            return root\n        queue = []\n        queue.append(root)\n        while queue:\n            dq = collections.deque()\n            length = len(queue)\n            for i in range(length):\n                curr = queue.pop(0)\n                dq.append(curr)\n                if curr.left:\n                    queue.append(curr.left)\n                if curr.right:\n                    queue.append(curr.right)\n            while len(dq) > 1:\n                popped = dq.popleft()\n                popped.next = dq[0]\n            popped = dq.popleft()\n            popped.next = None\n        return root\n```\n---\n>### *Please don\'t forget to upvote if you\'ve liked my solution.* \u2B06\uFE0F\n---
['Breadth-First Search', 'Python', 'C++', 'Java', 'Python3']