Go, Vantage point
가까운 곳을 걷지 않고 서는 먼 곳을 갈 수 없다.
Github | https://github.com/overnew/
Blog | https://everenew.tistory.com/
티스토리 뷰
[LeetCode] 297. Serialize and Deserialize Binary Tree (C++)
EVEerNew 2023. 3. 5. 23:10문제
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만 지키면 되기 때문에 자유도는 상당히 높다.
코드
'알고리즘 공부 > LeetCode' 카테고리의 다른 글
| [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal (C++) (0) | 2023.07.04 |
|---|---|
| [LeetCode] 572. Subtree of Another Tree (C++) (0) | 2023.07.02 |
| [LeetCode] 102. Binary Tree Level Order Traversal (C++) (0) | 2022.12.30 |
| [LeetCode] 124. Binary Tree Maximum Path Sum (C++) (0) | 2022.12.28 |
| [LeetCode] 5. Longest Palindromic Substring (C++) (0) | 2022.12.27 |