✔️ 100% Fastest Swift Solution, time: O(n), space: O(1).
\n/**\n * Definition for a Node.\n * public class Node {\n *     public var val: Int\n *     public var left: Node?\n *     public var right: Node?\n *\t   publ
```\n/**\n * Definition for a Node.\n * public class Node {\n *     public var val: Int\n *     public var left: Node?\n *     public var right: Node?\n *\t   public var next: Node?\n *     public init(_ val: Int) {\n *         self.val = val\n *         self.left = nil\n *         self.right = nil\n *         self.next = nil\n *     }\n * }\n */\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\nLet me know in comments if you have any doubts. I will be happy to answer.\n\nPlease upvote if you found the solution useful.
['Swift']