297. 二叉树的序列化与反序列化

letcode 297题思路

  1. 二叉树的序列化与反序列化 
    序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构雅思分数

示例:

你可以将以下二叉树:

                      1   /  \  2   3   /  \  4   5   

序列化为 “[1,2,3,null,null,4,5]”

题解思路:

  • BFS
                      class   Codec   :   def   serialize   (  self  ,  root  )   :   """Encodes a tree to a single string.
        
        :type root: TreeNode
        :rtype: str
        """   if   (  root  is   None   )   :   return   [   ]  res  =   [   ]   from  collections  import  deque
        queue  =  deque  (   [  root  ]   )   while   (  queue  )   :  tmp  =  queue  .  popleft  (   )   if   (  tmp  is   None   )   :  res  .  append  (   '#'   )   else   :  res  .  append  (   str   (  tmp  .  val  )   )  queue  .  append  (  tmp  .  left  )  queue  .  append  (  tmp  .  right  )   return   ','   .  join  (  res  )   def   deserialize   (  self  ,  data  )   :   """Decodes your encoded data to tree.
        
        :type data: str
        :rtype: TreeNode
        """   from  collections  import  deque  if   (  data  ==   ''   or  data  ==   [   ]   )   :   return   None  queue  =  deque  (   [   ]   )  data  =  data  .  split  (   ','   )  root  =  TreeNode  (  data  [   0   ]   )  queue  .  append  (  root  )  i  =   1   while   (  queue  )   :  tmp  =  queue  .  popleft  (   )   if   (  data  [  i  ]   !=   '#'   )   :  tmp  .  left  =  TreeNode  (  data  [  i  ]   )  queue  .  append  (  tmp  .  left  )  i  +=   1   if   (  data  [  i  ]   !=   '#'   )   :  tmp  .  right  =  TreeNode  (  data  [  i  ]   )  queue  .  append  (  tmp  .  right  )  i  +=   1   return  root  
  • 递归思路
                      class   Codec   :   def   serialize   (  self  ,  root  )   :   """Encodes a tree to a single string.
        
        :type root: TreeNode
        :rtype: str
        """  res  =   [   ]   def   helper   (  root  )   :   if   (   not  root  )   :  res  .  append  (   '#'   )   return  res  .  append  (   str   (  root  .  val  )   )  helper  (  root  .  left  )  helper  (  root  .  right  )  helper  (  root  )   return   ','   .  join  (  res  )   def   deserialize   (  self  ,  data  )   :   """Decodes your encoded data to tree.
        
        :type data: str
        :rtype: TreeNode
        """  data  =  data  .  split  (   ','   )   def   helper   (  index  )   :   if   (  data  [  index  ]   ==   '#'   )   :   return   (  index  ,   None   )   else   :  root  =  TreeNode  (  data  [  index  ]   )  l  ,  root  .  left  =  helper  (   1   +  index  )  r  ,  root  .  right  =  helper  (  l  +   1   )   return   (  r  ,  root  )  l  ,  root  =  helper  (   0   )   return  root  
  • 巧用next方法
                      class   Codec   :   def   serialize   (  self  ,  root  )   :   """Encodes a tree to a single string.
        
        :type root: TreeNode
        :rtype: str
        """  res  =   [   ]   def   helper   (  root  )   :   if   (   not  root  )   :  res  .  append  (   '#'   )   return  res  .  append  (   str   (  root  .  val  )   )  helper  (  root  .  left  )  helper  (  root  .  right  )  helper  (  root  )   return   ','   .  join  (  res  )   def   deserialize   (  self  ,  data  )   :   """Decodes your encoded data to tree.
        
        :type data: str
        :rtype: TreeNode
        """  vals  =   iter   (  data  .  split  (   ','   )   )   def   helper   (   )   :  a  =   next   (  vals  )   if   (  a  ==   '#'   )   :   return   None   else   :  root  =  TreeNode  (  a  )   print   (  root  .  val  )  root  .  left  =  helper  (   )  root  .  right  =  helper  (   )   return  root
        root  =  helper  (   )   return  root  
posted @ 2020-09-29 16:04  小琪琪来啦  阅读(4)  评论(0编辑  收藏  举报
欢迎大家来到我的博客:武汉雅思 | 武汉托福 | 新航道 | 雅思培训 | dnfsf | 天龙sf | 热血江湖sf | 天龙sf | dnfsf