leetcode 297. Serialize and Deserialize Binary Tree

逃离我推掉我的手 2022-07-26 11:26 186阅读 0赞

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.

For example, you may serialize the following tree

  1. 1
  2. / \
  3. 2 3
  4. / \
  5. 4 5

as "[1,2,3,null,null,4,5]" , just the same as how LeetCode OJ serializes a binary tree . You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

  1. class Codec {
  2. string my_itoa(int n)
  3. {
  4. if (n == 0)
  5. return "0";
  6. bool positive = n < 0 ? false : true;
  7. n = abs(n);
  8. string re;
  9. while (n != 0)
  10. {
  11. re.insert(re.begin(), n % 10 + '0');
  12. n /= 10;
  13. }
  14. if (!positive)
  15. re.insert(re.begin(), '-');
  16. return re;
  17. }
  18. int my_atoi(string s)
  19. {
  20. bool posivtive = s.front() != '-';
  21. if (s.front() == '-')
  22. s.erase(0, 1);
  23. int re = 0;
  24. while (!s.empty())
  25. {
  26. re = 10 * re + s[0] - '0';
  27. s.erase(0, 1);
  28. }
  29. if (!posivtive)
  30. re = -re;
  31. return re;
  32. }
  33. public:
  34. // Encodes a tree to a single string.
  35. string serialize(TreeNode* root) {
  36. if (root == NULL)
  37. return "#";
  38. string s;
  39. vector<TreeNode*>que;
  40. que.push_back(root);
  41. while (!que.empty())
  42. {
  43. TreeNode*n = que.back();
  44. que.pop_back();
  45. if (n == NULL)
  46. s += "#,";
  47. else
  48. {
  49. s += my_itoa(n->val)+',';
  50. que.push_back(n->right);
  51. que.push_back(n->left);
  52. }
  53. }
  54. if (s.back() == ',')
  55. s.pop_back();
  56. return s;
  57. }
  58. // Decodes your encoded data to tree.
  59. TreeNode* deserialize(string data) {
  60. if (data == "#")
  61. return NULL;
  62. TreeNode*root = NULL;
  63. int pos = data.find(',');
  64. vector<TreeNode*>que;
  65. while (!data.empty())
  66. {
  67. string s;
  68. if (pos != string::npos)
  69. {
  70. s = string(data.begin(), data.begin() + pos);
  71. data.erase(0, pos + 1);
  72. }
  73. else
  74. {
  75. s = data;
  76. data.clear();
  77. }
  78. if (s == "#")
  79. {
  80. if (que.back()->left != NULL)
  81. {
  82. que.pop_back();
  83. pos = data.find(',');
  84. continue;
  85. }
  86. pos = data.find(',');
  87. if (pos != string::npos)
  88. {
  89. s = string(data.begin(), data.begin() + pos);
  90. data.erase(0, pos + 1);
  91. }
  92. else
  93. {
  94. s = data;
  95. data.clear();
  96. }
  97. if (s == "#")
  98. que.pop_back();
  99. else
  100. {
  101. int val = my_atoi(s);
  102. TreeNode*n = new TreeNode(val);
  103. que.back()->right = n;
  104. que.pop_back();
  105. que.push_back(n);
  106. }
  107. }
  108. else
  109. {
  110. int val = my_atoi(s);
  111. if (root == NULL)
  112. {
  113. root = new TreeNode(val);
  114. que.push_back(root);
  115. }
  116. else
  117. {
  118. TreeNode*n = new TreeNode(val);
  119. if (que.back()->left == NULL)
  120. {
  121. que.back()->left = n;
  122. que.push_back(n);
  123. }
  124. else
  125. {
  126. que.back()->right = n;
  127. que.pop_back();
  128. que.push_back(n);
  129. }
  130. }
  131. }
  132. pos = data.find(',');
  133. }
  134. return root;
  135. }
  136. };

accept

发表评论

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

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

相关阅读