https://leetcode.com/problems/flatten-binary-tree-to-linked-list/

func flatten(_ root: TreeNode?) {
    guard let r = root else { return }
    flatten(r.left)
    flatten(r.right)
    let tmp = r.right
    r.right = r.left
    r.left = nil
    var last: TreeNode? = r
    while last?.right != nil {
        last = last?.right
    }
    last?.right = tmp
}

Runtime: 16 ms, faster than 100.00% of Swift online submissions for Flatten Binary Tree to Linked List.
Memory Usage: 19.1 MB, less than 50.00% of Swift online submissions for Flatten Binary Tree to Linked List.