Skip to content

Commit e108b8d

Browse files
committed
Added PreOrder Approach
Leetcode Accepted
1 parent 361729d commit e108b8d

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Serialize and Deserialize Binary Tree
2+
# https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode(object):
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.left = None
9+
# self.right = None
10+
11+
class Codec:
12+
13+
def preOrder(self, node, array):
14+
if not node:
15+
array.append("null")
16+
return
17+
array.append(str(node.val))
18+
self.preOrder(node.left, array)
19+
self.preOrder(node.right, array)
20+
21+
def serialize(self, root):
22+
array = []
23+
self.preOrder(root, array)
24+
# Join the result using comma as delimiter.
25+
return ",".join(array)
26+
"""Encodes a tree to a single string.
27+
:type root: TreeNode
28+
:rtype: str
29+
"""
30+
def createNode(self, val):
31+
return TreeNode(int(val))
32+
33+
def preOrderCreateTree(self, array, index):
34+
# The reason why we use index as list and not a single number is because a single number gets modified within recurssive calls but a list does not.
35+
if (index[0] > len(array) or array[index[0]] == 'null'):
36+
return
37+
# Create the node with the value.
38+
node = self.createNode(array[index[0]])
39+
# Increment the index and get the left side of the node.
40+
index[0] = index[0] + 1
41+
node.left = self.preOrderCreateTree(array, index)
42+
# Increment the index and get the right side of the node.
43+
index[0] = index[0] + 1
44+
node.right = self.preOrderCreateTree(array, index)
45+
# Return this node, so it can be added to left or right of the calling parent.
46+
return node
47+
48+
def deserialize(self, data):
49+
if not data:
50+
return []
51+
# Split the data into a list, using comma as delimiter.
52+
nodeList = data.split(",")
53+
# Create the tree.
54+
node = self.preOrderCreateTree(nodeList, [0])
55+
# Return the root of the tree.
56+
return node
57+
"""Decodes your encoded data to tree.
58+
:type data: str
59+
:rtype: TreeNode
60+
"""
61+
62+
63+
# Your Codec object will be instantiated and called as such:
64+
# ser = Codec()
65+
# deser = Codec()
66+
# ans = deser.deserialize(ser.serialize(root))

Blind 75/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,16 @@
222222
<li><a href="Programs/Binary Tree Level Order Traversal.py">Binary Tree Level Order Traversal</a>
223223
<p><b>Approach</b>: Used a modified iterative BFS</p>
224224
</li>
225-
<li>Serialize and Deserialize Binary Tree
225+
<li><a href="Programs/Serialize and Deserialize Binary Tree - PreOrder.py">Serialize and Deserialize Binary Tree</a>
226+
<p><b>Approach</b>: Use preorder to serialize it using comma as delimiter. <br/>
227+
Then use similar method we use to find height of tree to first add left child of sub tree and then add right child of sub tree, all while traversing the array.
228+
</p>
226229
<p><b>Thoughts</b>: Initially I thought of using tries to do this, but it can create problems if all nodes of the tree have same value. <br/>
227230
My next thought was to do inOrder and preOrder traversal and then build the tree, but I was plagued by the same above problem, what if all tree nodes have same value. <br/>
228231
Then I thought about level order traversal but use delimiter for null nodes, this will waste a LOT of space (what if the tree is skewed), but I will end up with correct answer. <br/>
229232
</p>
230233
<ul>
231-
<li><a href="proxy.php?url=https%3A%2F%2Fgithub.com%2FPrograms%2FSerialize+and+Deserialize+Binary+Tree+-+LevelOrder+Toomuch+Space.py">Too much space with level order</a> </li>
234+
<li><a href="proxy.php?url=https%3A%2F%2Fgithub.com%2FPrograms%2FSerialize+and+Deserialize+Binary+Tree+-+LevelOrder+Toomuch+Space.py">Too much space with level order</a>: This exceeded leetcode space limit. </li>
232235
</ul>
233236
</li>
234237
<li>Subtree of Another Tree</li>

0 commit comments

Comments
 (0)