LeetCode - Medium - 449. Serialize and Deserialize BST

亦凉 2022-10-08 11:08 73阅读 0赞

Topic

  • Tree

Description

https://leetcode.com/problems/serialize-and-deserialize-bst/

Serialization is 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 search tree. There is no restriction on how your serialization/deserialization algorithm should work. You need to ensure that a binary search tree can be serialized to a string, and this string can be deserialized to the original tree structure.

The encoded string should be as compact as possible.

Example 1:

  1. Input: root = [2,1,3]
  2. Output: [2,1,3]

Example 2:

  1. Input: root = []
  2. Output: []

Constraints:

  • The number of nodes in the tree is in the range [ 0 , 1 0 4 ] [0, 10^4] [0,104].
  • 0 < = N o d e . v a l < = 1 0 4 0 <= Node.val <= 10^4 0<=Node.val<=104
  • The input tree is guaranteed to be a binary search tree.

Analysis

由LeetCode - Medium - 1008. Construct Binary Search Tree from Preorder Traversal得到启发。

序列化:用前序遍历根节点。

反序列化:将前序遍历的序列按照BST通常插入法重建BST。

Submission

  1. import java.util.LinkedList;
  2. import com.lun.util.BinaryTree.TreeNode;
  3. public class SerializeAndDeserializeBST {
  4. public static class Codec {
  5. // Encodes a tree to a single string.
  6. public String serialize(TreeNode root) {
  7. if(root == null) return "";
  8. LinkedList<TreeNode> stack = new LinkedList<>();
  9. StringBuilder sb = new StringBuilder();
  10. stack.push(root);
  11. while(!stack.isEmpty()) {
  12. TreeNode node = stack.pop();
  13. sb.append(node.val);
  14. sb.append(',');
  15. if(node.right != null)
  16. stack.push(node.right);
  17. if(node.left != null)
  18. stack.push(node.left);
  19. }
  20. return sb.substring(0, sb.length() - 1);
  21. }
  22. // Decodes your encoded data to tree.
  23. public TreeNode deserialize(String data) {
  24. if(data == null || data.strip().isEmpty())
  25. return null;
  26. int value = 0;
  27. TreeNode root = null, p, parent;
  28. for(int i = 0; i <= data.length(); i++) {
  29. if(i == data.length() || data.charAt(i) == ',') {
  30. if(root == null) {
  31. root = new TreeNode(value);
  32. }else {
  33. p = root;
  34. parent = null;
  35. while(p != null) {
  36. parent = p;
  37. if(value < p.val) {
  38. p = p.left;
  39. }else if(p.val < value){
  40. p = p.right;
  41. }
  42. }
  43. if(value < parent.val)
  44. parent.left = new TreeNode(value);
  45. else
  46. parent.right = new TreeNode(value);
  47. }
  48. value = 0;
  49. }else {
  50. int num = data.charAt(i) - '0';
  51. value = value * 10 + num;
  52. }
  53. }
  54. return root;
  55. }
  56. }
  57. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.medium.SerializeAndDeserializeBST.Codec;
  4. import com.lun.util.BinaryTree;
  5. import com.lun.util.BinaryTree.TreeNode;
  6. public class SerializeAndDeserializeBSTTest {
  7. @Test
  8. public void test() {
  9. // SerializeAndDeserializeBST sObj = new SerializeAndDeserializeBST();
  10. TreeNode original = BinaryTree.integers2BinaryTree(2, 1, 3);
  11. Codec ser = new Codec();
  12. Codec deser = new Codec();
  13. String tree = ser.serialize(original);
  14. assertEquals("2,1,3", tree);
  15. TreeNode ans = deser.deserialize(tree);
  16. assertTrue(BinaryTree.equals(ans, original));
  17. String tree2 = ser.serialize(null);
  18. assertEquals("", tree2);
  19. TreeNode ans2 = deser.deserialize(tree2);
  20. assertNull(ans2);
  21. }
  22. }

发表评论

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

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

相关阅读