Skip to content

Commit 87a5242

Browse files
committed
added Serialize & Deserialize Binary Tree (Hard)
1 parent eee5afc commit 87a5242

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Serialize and Deserialize Binary Tree
2+
3+
[Leetcode Link](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)
4+
5+
## Problem:
6+
7+
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 network connection link to be reconstructed later in the same or another computer environment.
8+
9+
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
10+
11+
**Clarification:** The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
12+
13+
## Example:
14+
15+
![example](assets/serdeser.jpg)
16+
17+
```
18+
Input: root = [1,2,3,null,null,4,5]
19+
Output: [1,2,3,null,null,4,5]
20+
```
21+
22+
## Note:
23+
24+
- The number of nodes in the tree is in the range [0, 104].
25+
- -1000 <= Node.val <= 1000
14.6 KB
Loading
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from typing import List
2+
3+
# Definition for a binary tree node.
4+
5+
6+
class TreeNode(object):
7+
def __init__(self, x, left=None, right=None):
8+
self.val = x
9+
self.left = left
10+
self.right = right
11+
12+
def __str__(self):
13+
tree = list()
14+
queue = list()
15+
queue.append(self)
16+
while queue and not all(node is None for node in queue):
17+
currentNode = queue.pop(0)
18+
if currentNode is None:
19+
tree.append(None)
20+
else:
21+
tree.append(currentNode.val)
22+
queue.append(currentNode.left)
23+
queue.append(currentNode.right)
24+
return str(tree)
25+
26+
27+
class Codec:
28+
29+
def serialize(self, root):
30+
"""Encodes a tree to a single string.
31+
32+
:type root: TreeNode
33+
:rtype: str
34+
"""
35+
preorderList = list()
36+
self.preorder(root, preorderList)
37+
return(str(preorderList))
38+
39+
def preorder(self, root, preorderList):
40+
if root is None:
41+
# if preorderList[len(preorderList)-1] is not None:
42+
preorderList.append(None)
43+
return
44+
preorderList.append(root.val)
45+
self.preorder(root.left, preorderList)
46+
self.preorder(root.right, preorderList)
47+
48+
def deserialize(self, data):
49+
"""Decodes your encoded data to tree.
50+
51+
:type data: str
52+
:rtype: TreeNode
53+
"""
54+
data = eval(data)
55+
return self.recursiveDeserialize(data)
56+
57+
def recursiveDeserialize(self, data: List[int]) -> TreeNode:
58+
if len(data) <= 0:
59+
return
60+
currentVal = data.pop(0)
61+
if currentVal is None:
62+
return
63+
root = TreeNode(currentVal)
64+
root.left = self.recursiveDeserialize(data)
65+
root.right = self.recursiveDeserialize(data)
66+
return root
67+
68+
69+
# Your Codec object will be instantiated and called as such:
70+
# ser = Codec()
71+
# deser = Codec()
72+
# ans = deser.deserialize(ser.serialize(root))
73+
74+
sol = Codec()
75+
# generate test tree [1,2,3,null,null,4,5]
76+
root = TreeNode(1, TreeNode(2), TreeNode(3, TreeNode(4), TreeNode(5)))
77+
78+
print("Original Tree:", root)
79+
80+
ser = sol.serialize(root)
81+
print("Serialization using preorder:", ser)
82+
print("Deserialized tree:", sol.deserialize(ser))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Languages used: Java and Python
9292
- [Insert Interval](Hard/InsertInterval)
9393
- [Find Median From Data Stream](Hard/FindMedianFromDataStream)
9494
- [Escape a Large Maze](Hard/EscapeLargeMaze)
95+
- [Serialize and Deserialize Binary Tree](Hard/SerializeAndDeserializeBinaryTree)
9596

9697
---
9798

0 commit comments

Comments
 (0)