Algorithm
本周选择的算法题是:Serialize and Deserialize Binary Tree。
规则
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.
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.
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.
Example 1:
Input: root = [1,2,3,null,null,4,5]
Output: [1,2,3,null,null,4,5]
Example 2:
Input: root = []
Output: []
Constraints:
- The number of nodes in the tree is in the range
[0, 104]
. -1000 <= Node.val <= 1000
Solution
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
if not root: return ""
ans = []
stack = [root]
while stack:
node = stack.pop(0)
if node:
ans.append(str(node.val))
stack.append(node.left)
stack.append(node.right)
else:
ans.append("#")
return " ".join(ans)
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
if not data: return None
vals = data.split()
root = TreeNode(int(vals[0]))
stack, i = [root], 1
while i < len(vals):
parent = stack.pop(0)
if vals[i] != '#':
parent.left = TreeNode(int(vals[i]))
stack.append(parent.left)
i += 1
if vals[i] != '#':
parent.right = TreeNode(int(vals[i]))
stack.append(parent.right)
i += 1
return root
Review
How Focusing on Intentions Rather than Resolutions Can Help Create a Better Work-Life Balance
如果刻意寻求工作与生活的平衡大概是很难找到结果的,工作、生活和身体是一个整体,与其定义边界,不如直面最核心的问题:如何享受这一生。
我看到越来越多的意见领袖在谈论正念和冥想,惭愧的是自己还没有去认真思考背后的原因,也许如作者所说,无论是工作还是生活,我们应该追求 intentions 而不是 resolutions,intentions 侧重于正向的趋势,不会让我们与结果强关联起来,focus on the now rather than the outcome。
Tip
优化了 CodingTour 搜索体验。
Share
本周探讨 OKR 比较多,没有特别值得分享的地方,放一个郭东白老师课程里的观点吧~
在每个架构规划启动之前,应该有且仅有一个正确的目标,这是架构设计的起点。目标不正确,你和你的团队再努力都没办法成功。目标的重要性,就在于它能够一直引导我们走在正确的方向上,同时帮助我们做取舍,在多个备选架构方案中作出最优的选择。