【LeetCode 297】二叉树的序列化与反序列化

Dear 丶 2022-11-30 01:42 247阅读 0赞

题目描述

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

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

示例:

你可以将以下二叉树:

  1. 1
  2. / \
  3. 2 3
  4. / \
  5. 4 5
  6. 序列化为 "[1,2,3,null,null,4,5]"

提示: 这与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。

说明: 不要使用类的成员 / 全局 / 静态变量来存储状态,你的序列化和反序列化算法应该是无状态的。

解题思路

对于这道二叉树的题目,我们能想到的最直接的方式就是深度优先宾利和广度优先遍历,这里就分别用两种方式来解答。

深度优先遍历:

  • 首先是序列化二叉树,可以定义一个遍历方法,先访问根节点,再访问左节点,最后访问右节点,将每个节点的值都存入数组,如果是null也要存入数组。
  • 之后是反序列化二叉树,也就是将数组转化为二叉树,因为数组是二叉树先序遍历的结果,所以我们就可以遍历数组,然后按照根节点、左子树、右子树的顺序复原二叉树

使用深度优先遍历的方法的时间复杂度为O(n),空间复杂度为O(n)。

广度优先遍历:

  • 首先是序列化二叉树,广度优先遍历的遍历顺序是按照层级从上往下遍历(层序遍历),所以我们可以利用队列先进先出的特性,维持一个数组。先将根节点入队,再将左节点和右节点入队,递归即可。
  • 之后是反序列化二叉树,我们可以从数组中取出第一个元素生成根节点,将根节点加入队列,循环队列,将根节点的左右子树分别加入队列,循环此操作,直至队列为空。其中队列中的节点用于后面遍历其左右子节点。

使用广度优先遍历的方法的时间复杂度为O(n),空间复杂度为O(n)。

代码实现

深度优先遍历:

  1. /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */
  2. /** * Encodes a tree to a single string. * * @param {TreeNode} root * @return {string} */
  3. var serialize = function(root) {
  4. const result = []
  5. function traverseNode(node){
  6. if(node === null){
  7. result.push(null)
  8. }else{
  9. result.push(node.val)
  10. traverseNode(node.left)
  11. traverseNode(node.right)
  12. }
  13. }
  14. traverseNode(root)
  15. return result
  16. };
  17. /** * Decodes your encoded data to tree. * * @param {string} data * @return {TreeNode} */
  18. var deserialize = function(data) {
  19. const len = data.length
  20. if(!len){
  21. return null
  22. }
  23. let i = 0
  24. function structure (){
  25. // 递归停止条件
  26. if(i >= len){
  27. return null
  28. }
  29. const val = data[i]
  30. i++
  31. if(val === null){
  32. return null
  33. }
  34. const node = new TreeNode(val)
  35. node.left = structure()
  36. node.right = structure()
  37. return node
  38. }
  39. return structure()
  40. };
  41. /** * Your functions will be called as such: * deserialize(serialize(root)); */

广度优先遍历:

  1. /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */
  2. /** * Encodes a tree to a single string. * * @param {TreeNode} root * @return {string} */
  3. var serialize = function(root) {
  4. if(!root){
  5. return []
  6. }
  7. const result = []
  8. const queue = []
  9. queue.push(root)
  10. let node ;
  11. while(queue.length){
  12. node = queue.shift()
  13. result.push(node ? node.val : null)
  14. if(node){
  15. queue.push(node.left)
  16. queue.push(node.right)
  17. }
  18. }
  19. return result
  20. };
  21. /** * Decodes your encoded data to tree. * * @param {string} data * @return {TreeNode} */
  22. var deserialize = function(data) {
  23. const len = data.length
  24. if(!len){
  25. return null
  26. }
  27. const root = new TreeNode(data.shift())
  28. const queue = [root]
  29. while(queue.length){
  30. // 取出将要遍历的节点
  31. const node = queue.shift()
  32. if(!data.length){
  33. break
  34. }
  35. // 还原左节点
  36. const leftVal = data.shift()
  37. if(leftVal === null){
  38. node.left = null
  39. }else{
  40. node.left = new TreeNode(leftVal)
  41. queue.push(node.left)
  42. }
  43. if(!data.length){
  44. break
  45. }
  46. // 还原右节点
  47. const rightVal = data.shift()
  48. if(rightVal === null){
  49. node.right = null
  50. }else{
  51. node.right = new TreeNode(rightVal)
  52. queue.push(node.right)
  53. }
  54. }
  55. return root
  56. };
  57. /** * Your functions will be called as such: * deserialize(serialize(root)); */

提交结果

深度优先遍历:
在这里插入图片描述
广度优先遍历:

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,247人围观)

还没有评论,来说两句吧...

相关阅读

    相关 序列/序列

    二叉树被记录成文件的过程,为二叉树的序列化 通过文件重新建立原来的二叉树的过程,为二叉树的反序列化 设计方案并实现。 (已知结点类型为32位整型) 思路:先序遍历实现。