Swift solution


  • 0
    class Solution {
        func connect(_ root: TreeLinkNode?) {
            guard let root = root else {
                return
            }
            
            var prev = root
            var current: TreeLinkNode? = nil
            
            while prev.left != nil {
                current = prev
                while current != nil {
                    current?.left?.next = current?.right
                    if current?.next != nil {
                        current?.right?.next = current?.next?.left
                    }
                    current = current?.next
                }
                prev = prev.left!
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.