File: root - text - article - 2019 - 12 - tree-serialization.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 700 Views, 19515 Search Bots | 171 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 700 Views, 19515 Search Bots | 171 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by Apple:
You are given the root of a binary tree. You need to implement 2 functions:
1. serialize(root) which serializes the tree into a string representation
2. deserialize(s) which deserializes the string back to the original tree that it represents
For this problem, often you will be asked to design your own serialization format. However, for simplicity, let's use the pre-order traversal of the tree. Here's your starting point:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 700 Views, 19515 Search Bots | 171 Words You are given the root of a binary tree. You need to implement 2 functions:
1. serialize(root) which serializes the tree into a string representation
2. deserialize(s) which deserializes the string back to the original tree that it represents
For this problem, often you will be asked to design your own serialization format. However, for simplicity, let's use the pre-order traversal of the tree. Here's your starting point:
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __str__(self):
# pre-order printing of the tree.
result = ''
result += str(self.val)
if self.left:
result += str(self.left)
if self.right:
result += str(self.right)
return result
def serialize(root):
# Fill this in.
def deserialize(data):
# Fill this in.
# 1
# / \
# 3 4
# / \ \
# 2 5 7
tree = Node(1)
tree.left = Node(3)
tree.left.left = Node(2)
tree.left.right = Node(5)
tree.right = Node(4)
tree.right.right = Node(7)
print serialize(tree)
# 1 3 2 # # 5 # # 4 # 7 # #
print deserialize('1 3 2 # # 5 # # 4 # 7 # #')
# 132547
Related Articles
- Algorithm Interview: Smallest Number that is not a Sum of a Subset of List
- Daily Interview Problem: Tree Serialization
- Algorithm Interview Question: Nth Fibonacci Number
- [Daily Problem] Add two numbers as a linked list
- Algorithm Interview: Maximum Path Sum in Binary Tree
- CVE-2015-8874 - cPanel EasyApache Vulnerabilities
- Daily Interview Problem: Longest Substring With K Distinct Characters
- Algorithm Interview: String Compression
- Daily Interview Problem: Contiguous Subarray with Maximum Sum
- Daily Interview Problem: Largest Product of 3 Elements I
©2006~2024 SteakOverCooked - 0.00895 Seconds(s) - 2171.412 KB/s - 30 Online Memory: 493.01 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !