본문 바로가기
알고리즘

[Swift] LeetCode - 297. Serialize and Deserialize Binary Tree

by 고고 2022. 2. 17.

문제: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/

 

Serialize and Deserialize Binary Tree - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

null이면 #을 넣어 직렬화하였습니다.

class Codec {
    func serialize(_ root: TreeNode?) -> String {
        var queue = [root]
        var result = [String]()
        
        while !queue.isEmpty {
            let node = queue.removeFirst()
            
            if node == nil {
                result.append("#")
            } else {
                queue.append(node!.left)
                queue.append(node!.right)
                
                result.append(String(node!.val))
            }
        }
        
        return result.joined(separator: " ")
    }
    
    func deserialize(_ data: String) -> TreeNode? {
        let nodes = data.split(separator: " ").map { String($0) }
        
        if nodes[0] == "#" { return nil }
        
        let root = TreeNode(Int(nodes[0])!)
        var queue = [root]
        var index = 1
        
        while !queue.isEmpty {
            let node = queue.removeFirst()
            
            if nodes[index] != "#" {
                node.left = TreeNode(Int(nodes[index])!)
                queue.append(node.left!)
            }
            index += 1
            
            if nodes[index] != "#" {
                node.right = TreeNode(Int(nodes[index])!)
                queue.append(node.right!)
            }
            index += 1
        }
        
        return root
    }
}

댓글