题解 | #序列化二叉树#
序列化二叉树
https://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 核心点:二叉树的遍历 list1 = [] def Serialize(self, root): # write code here if root == None: self.list1.append('#') return self.list1 self.list1.append(root.val) self.Serialize(root.left) self.Serialize(root.right) return self.list1 start=-1 def Deserialize(self, s): # write code here self.start=self.start+1 if self.start>=len(s) or s[self.start]=="#" or s==None : return cur=TreeNode(int(s[self.start])) cur.left=self.Deserialize(s) cur.right=self.Deserialize(s) return cur
剑指offer刷题笔记 文章被收录于专栏
24秋招剑指offer刷题的笔记