일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- kaggle
- t1
- concurrency
- Python Implementation
- Regular Expression
- 파이썬
- shiba
- DWG
- 43. Multiply Strings
- 운영체제
- 315. Count of Smaller Numbers After Self
- Python Code
- 컴퓨터의 구조
- Protocol
- Generator
- Convert Sorted List to Binary Search Tree
- 715. Range Module
- Class
- Substring with Concatenation of All Words
- attribute
- 109. Convert Sorted List to Binary Search Tree
- 프로그래머스
- iterator
- LeetCode
- Decorator
- Python
- data science
- 밴픽
- 시바견
- 30. Substring with Concatenation of All Words
Archives
- Today
- Total
Scribbling
LeetCode: 297. Serialize and Deserialize Binary Tree 본문
Computer Science/Coding Test
LeetCode: 297. Serialize and Deserialize Binary Tree
focalpoint 2022. 1. 2. 19:08My somewhat complicated code using queue.
Both time and memory complexities are O(N).
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
if not root:
return ''
ret = str(root.val) + ' '
q = [root]
while q:
next_q = []
for node in q:
if node.left:
next_q.append(node.left)
ret += str(node.left.val) + ' '
else:
ret += 'null '
if node.right:
next_q.append(node.right)
ret += str(node.right.val) + ' '
else:
ret += 'null '
q = next_q
return ret[:-1]
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
if not data:
return None
data = data.split(' ')
root, j = TreeNode(val=int(data[0])), 1
q = [root]
while q:
next_q = []
for node in q:
if data[j] != 'null':
node.left = TreeNode(val=int(data[j]))
next_q.append(node.left)
j += 1
if data[j] != 'null':
node.right = TreeNode(val=int(data[j]))
next_q.append(node.right)
j += 1
q = next_q
return root
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# ans = deser.deserialize(ser.serialize(root))
Similar idea but much simpler code with preorder recursive.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from collections import deque
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
if not root:
return 'n'
return ','.join([str(root.val), self.serialize(root.left), self.serialize(root.right)])
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
q = deque()
q.extend(data.split(","))
self.data = q
return self.deserialize_helper()
def deserialize_helper(self):
if self.data[0] == 'n':
self.data.popleft()
return None
node = TreeNode(self.data.popleft())
node.left = self.deserialize_helper()
node.right = self.deserialize_helper()
return node
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# ans = deser.deserialize(ser.serialize(root))
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 268. Missing Number (0) | 2022.01.04 |
---|---|
LeetCode: 1010. Pairs of Songs With Total Durations Divisible by 60 (0) | 2022.01.03 |
LeetCode: 328. Odd Even Linked List (0) | 2021.12.31 |
LeetCode: 289. Game of Life (0) | 2021.12.29 |
LeetCode: 287. Find the Duplicate Number (0) | 2021.12.28 |