O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022
\n\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\nTake care broth
```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* ***Java***\n```\nclass Solution {\n    public Node connect(Node root) {\n        if(root == null) return null;\n        if(root.left != null) root.left.next = root.right;\n        if(root.right != null && root.next != null) root.right.next = root.next.left;\n        connect(root.left);\n        connect(root.right);\n        return root;\n    }\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 7.0MB*** (beats 100.00% / 100.00%).\n* ***C++***\n```\nclass Solution {\npublic:\n    Node* connect(Node* root) {\n        if(root && root->left != NULL) {\n            root->left->next = root->right;\n            connect(root->left);\n            if(root->next != NULL) root->right->next = root->next->left;\n            connect(root->right);\n        }\n        \n        return root;\n    }\n};\n```\n\n```\n```\n\n```\n```\n\n\nThe best result for the code below is ***26ms / 12.2MB*** (beats 95.42% / 82.32%).\n* ***Python***\n```\nclass Solution:\n    def connect(self, root: \'Node\') -> \'Node\':\n        # edge case check\n        if not root:\n            return None\n        \n        node = root # create a pointer to the root node\n        \n        # iterate only until we have a new level (because the connections for Nth level are done when we are at N-1th level)\n        while node.left:\n            head = node\n            while head:\n                head.left.next = head.right\n                if head.next:\n                    head.right.next = head.next.left\n                    \n                head = head.next\n                \n            node = node.left\n\n        return root\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***51ms / 34.2MB*** (beats 100.00% / 84.12%).\n* ***JavaScript***\n```\nvar connect = function(root) {\n    let ptr = root;\n    while(root && root.left){\n        let temp = root;\n        while(temp) {\n            temp.left.next = temp.right;\n            temp.right.next = temp.next ? temp.next.left : null;\n            temp = temp.next;\n        }\n        root = root.left;\n    }\n    return ptr;\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***68ms / 44.2MB*** (beats 100.00% / 45.25%).\n* ***Kotlin***\n```\nclass Solution {\n    fun connect(root: Node?): Node? =\n        if (root?.left == null && root?.right == null) root  //at bottom of tree      \n        else root?.apply {  //connect next and recurse downwards\n            left?.next = right\n            right?.next = next?.left\n            connect(left)\n            connect(right)\n        }\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***12ms / 32.2MB*** (beats 95% / 84%).\n* ***Swift***\n```\nclass Solution {\n    // - Complexity:\n    //   - time: O(n), where n is the number of nodes in the binary tree.\n    //   - space: O(1), only constant space is used.\n\n    func connect(_ root: Node?) -> Node? {\n        var leftMost = root\n\n        while leftMost?.left != nil {\n            var head = leftMost\n            while head != nil {\n                head?.left?.next = head?.right\n                if let next = head?.next {\n                    head?.right?.next = next.left\n                }\n                head = head?.next\n            }\n            leftMost = leftMost?.left\n        }\n\n        return root\n    }\n\n}\n```\n\n```\n```\n\n```\n```\n\n***"Open your eyes. Expect us." - \uD835\uDCD0\uD835\uDCF7\uD835\uDCF8\uD835\uDCF7\uD835\uDD02\uD835\uDCF6\uD835\uDCF8\uD835\uDCFE\uD835\uDCFC***
['Swift', 'C', 'Python', 'C++', 'Java', 'Python3', 'Kotlin', 'JavaScript']