✅ [Python, C++,Java]|| Beginner level Solution||100%Faster||Simple-Short-Solution✅
Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\nAny suggestions and improvements are always welcome.\n___\n__\nQ
***Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\nAny suggestions and improvements are always welcome*.**\n___________________\n_________________\n***Q116. Populating Next Right Pointers in Each Node***\n\nYou are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:\n```\nstruct Node {\n  int val;\n  Node *left;\n  Node *right;\n  Node *next;\n}\n```\nPopulate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.\nInitially, all next pointers are set to NULL.\n____________________________________________________________________________________________________________________\n\n***Time complexity*** - O(n)\n***Space complexity*** - O(1)\n____________________________________________________________________________________________________________________\n____________________________________________________________________________________________________________________\n\u2705 **Java Code** :\n**Runtime**: 0 ms, faster than 100.00% of Java online submissions for Populating Next Right Pointers in Each Node.\n```\nclass Solution {\n    public Node connect(Node root) {\n        if(root == null) return root;\n        Node current = root;\n        while(current != null) {\n            Node level1stNode = current;\n            while(current != null) \n            {\n                if(current.left != null) \n                    current.left.next = current.right;\n                if(current.right != null && current.next != null) \n                    current.right.next = current.next.left;\n\n                current = current.next;\n            }\n            current = level1stNode.left;\n        }\n        return root;\n    }\n}\n```\n**Runtime:**  0ms\n**Memory Usage:**  42.2 MB\n\n____________________________________________________________________________________________________________________\n____________________________________________________________________________________________________________________\n\u2705 **Python  Code** :\n\n```\nclass Solution:\n    def connect(self, root: \'Optional[Node]\') -> \'Optional[Node]\':\n        if not root: return root        \n        self.current = root\n        while self.current:\n            self.level1stNode = self.current\n            while self.current:\n                if self.current.left:\n                    self.current.left.next = self.current.right\n                    \n                if self.current.right and self.current.next:\n                    self.current.right.next = self.current.next.left\n                    \n                self.current = self.current.next\n            self.current = self.level1stNode.left\n        return root\n```\n**Runtime:**  131ms\n**Memory Usage:**  13.8 MB\n\n____________________________________________________________________________________________________________________\n____________________________________________________________________________________________________________________\n\u2705 **C++  Code** :\n\n```\n\nclass Solution {\npublic:\n    Node* connect(Node* root) {\n        if(!root) return root;\n        Node* current = root;\n        while(current) {\n            Node* level1stNode = current;\n            while(current) \n            {\n                if(current->left)\n                    current->left->next = current->right;\n                if(current->right && current->next)\n                    current->right->next = current->next->left;\n\n                current = current->next;\n            }\n            current = level1stNode->left;\n        }\n        return root;\n    }\n};\n```\n**Runtime:**  41ms\n**Memory Usage:**  69.7MB\n____________________________________________________________________________________________________________________\n____________________________________________________________________________________________________________________\nIf you like the solution, please upvote \uD83D\uDD3C\nFor any questions, or discussions, comment below. \uD83D\uDC47\uFE0F
['Breadth-First Search', 'C', 'Python', 'Java']