[LeetCode] 297. Serialize and Deserialize Binary Tree (C++)
문제
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/
Serialize and Deserialize Binary Tree - LeetCode
Can you solve this real interview question? Serialize and Deserialize Binary Tree - Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a n
leetcode.com
풀이
난이도: Hard
Serialization(직렬화)은 데이터 구조나 대상 파일을 bit의 순서로 나타내서 파일이나 메모리 버퍼에 저장하는 과정이다. 이 bit 순서들을 네트워크로 전송하면 수신측은 Deserialization(비직렬화)로 데이터를 재구성할 수 있다.
이는 Encode와 Decode의 관계와 동일하다.
이번 문제는 이진 트리를 Serialize로 string 데이터로 만들고, string 데이터를 Deserialize로 이진 트리로 만드는 함수를 구현해 보는 것이다.
root=[1,2,3,null,null,4,5]
이때 string 데이터는 이진 트리를 BFS로 표현한 것을 확인할 수 있다.
따라서 BFS를 구현하는 queue 자료구조로 작성하면 된다.
단, 정답은 아래와 같은 함수 호출로 진행된다.
따라서 어떤식으로 serialize 하고 deserialize로 넘기든, output format만 지키면 되기 때문에 자유도는 상당히 높다.
코드