Skip to content

Commit 9f2cbbe

Browse files
committed
Solution of Inorder Successor and Tree Serialization and Deserialization
1 parent 693904a commit 9f2cbbe

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

200-300q/285.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
3+
Note: If the given node has no in-order successor in the tree, return null
4+
'''
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode(object):
8+
# def __init__(self, x):
9+
# self.val = x
10+
# self.left = None
11+
# self.right = None
12+
13+
class Solution(object):
14+
def inorderSuccessor(self, root, p):
15+
"""
16+
:type root: TreeNode
17+
:type p: TreeNode
18+
:rtype: TreeNode
19+
"""
20+
21+
if not root or not p:
22+
return None
23+
24+
if p.right:
25+
p = p.right
26+
while p.left:
27+
p = p.left
28+
return p
29+
30+
successor = None
31+
while root and root != p:
32+
if root.val > p.val:
33+
successor = root
34+
root = root.left
35+
else:
36+
root = root.right
37+
return successor

200-300q/297.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'''
2+
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.
3+
4+
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.
5+
6+
Example:
7+
8+
You may serialize the following tree:
9+
10+
1
11+
/ \
12+
2 3
13+
/ \
14+
4 5
15+
16+
as "[1,2,3,null,null,4,5]"
17+
'''
18+
19+
# Definition for a binary tree node.
20+
# class TreeNode(object):
21+
# def __init__(self, x):
22+
# self.val = x
23+
# self.left = None
24+
# self.right = None
25+
26+
class Codec:
27+
28+
def serialize(self, root):
29+
"""Encodes a tree to a single string.
30+
31+
:type root: TreeNode
32+
:rtype: str
33+
"""
34+
35+
def preorder(root):
36+
if root:
37+
seralizeTree.append(str(root.val) + ',')
38+
preorder(root.left)
39+
preorder(root.right)
40+
else:
41+
seralizeTree.append('#,')
42+
43+
seralizeTree = []
44+
preorder(root)
45+
return ''.join(seralizeTree)
46+
47+
48+
def deserialize(self, data):
49+
"""Decodes your encoded data to tree.
50+
51+
:type data: str
52+
:rtype: TreeNode
53+
"""
54+
55+
def buildTree(preorder):
56+
value = preorder.pop(0)
57+
if value == '#':
58+
return None
59+
60+
node = TreeNode(int(value))
61+
node.left = buildTree(preorder)
62+
node.right = buildTree(preorder)
63+
return node
64+
65+
preorder = data.split(',')[:-1]
66+
return buildTree(preorder)
67+
68+
69+
# Your Codec object will be instantiated and called as such:
70+
# codec = Codec()
71+
# codec.deserialize(codec.serialize(root))

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
2424
| # | Title | Solution | Difficulty |
2525
|---| ----- | -------- | ---------- |
2626
|300|[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | [Python](./200-300q/300.py)|Medium|
27+
|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree) | [Python](./200-300q/297.py)|Hard|
2728
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [Python](./200-300q/295.py)|Hard|
2829
|289|[Game of Life](https://leetcode.com/problems/game-of-life) | [Python](/200-300q/289.py)|Medium|
2930
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [Python](./200-300q/287.py)|Hard|
31+
|285|[Inorder Successor in BST](https://leetcode.com/problems/inorder-successor-in-bst) | [Python](./200-300q/285.py)|Medium|
32+
|283|[Move Zeros](https://leetcode.com/problems/move-zeroes)|[Python](./200-300q/283.py)|Easy|
3033
|279|[Perfect Squares](https://leetcode.com/problems/perfect-squares/) | [Python](./200-300q/279.py)|Medium|
3134
|268|[Missing Number](https://leetcode.com/problems/missing-number)|[Python](./200-300q/268.py)|Easy|
3235
|253|[Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii) | [Python](./200-300q/253.py)|Medium|

0 commit comments

Comments
 (0)