返回介绍

solution / 0400-0499 / 0449.Serialize and Deserialize BST / README_EN

发布于 2024-06-17 01:04:00 字数 7597 浏览 0 评论 0 收藏 0

449. Serialize and Deserialize BST

中文文档

Description

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:

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

Example 2:

Input: root = []
Output: []

 

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • 0 <= Node.val <= 104
  • The input tree is guaranteed to be a binary search tree.

Solutions

Solution 1

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, x):
#     self.val = x
#     self.left = None
#     self.right = None


class Codec:
  def serialize(self, root: Optional[TreeNode]) -> str:
    """Encodes a tree to a single string."""

    def dfs(root: Optional[TreeNode]):
      if root is None:
        return
      nums.append(root.val)
      dfs(root.left)
      dfs(root.right)

    nums = []
    dfs(root)
    return " ".join(map(str, nums))

  def deserialize(self, data: str) -> Optional[TreeNode]:
    """Decodes your encoded data to tree."""

    def dfs(mi: int, mx: int) -> Optional[TreeNode]:
      nonlocal i
      if i == len(nums) or not mi <= nums[i] <= mx:
        return None
      x = nums[i]
      root = TreeNode(x)
      i += 1
      root.left = dfs(mi, x)
      root.right = dfs(x, mx)
      return root

    nums = list(map(int, data.split()))
    i = 0
    return dfs(-inf, inf)


# Your Codec object will be instantiated and called as such:
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# tree = ser.serialize(root)
# ans = deser.deserialize(tree)
# return ans
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode(int x) { val = x; }
 * }
 */
public class Codec {
  private int i;
  private List<String> nums;
  private final int inf = 1 << 30;

  // Encodes a tree to a single string.
  public String serialize(TreeNode root) {
    nums = new ArrayList<>();
    dfs(root);
    return String.join(" ", nums);
  }

  // Decodes your encoded data to tree.
  public TreeNode deserialize(String data) {
    if (data == null || "".equals(data)) {
      return null;
    }
    i = 0;
    nums = Arrays.asList(data.split(" "));
    return dfs(-inf, inf);
  }

  private void dfs(TreeNode root) {
    if (root == null) {
      return;
    }
    nums.add(String.valueOf(root.val));
    dfs(root.left);
    dfs(root.right);
  }

  private TreeNode dfs(int mi, int mx) {
    if (i == nums.size()) {
      return null;
    }
    int x = Integer.parseInt(nums.get(i));
    if (x < mi || x > mx) {
      return null;
    }
    TreeNode root = new TreeNode(x);
    ++i;
    root.left = dfs(mi, x);
    root.right = dfs(x, mx);
    return root;
  }
}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// String tree = ser.serialize(root);
// TreeNode ans = deser.deserialize(tree);
// return ans;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Codec {
public:
  // Encodes a tree to a single string.
  string serialize(TreeNode* root) {
    if (!root) {
      return "";
    }
    string data = "";
    function<void(TreeNode*)> dfs = [&](TreeNode* root) {
      if (!root) {
        return;
      }
      data += to_string(root->val) + " ";
      dfs(root->left);
      dfs(root->right);
    };
    dfs(root);
    data.pop_back();
    return data;
  }

  // Decodes your encoded data to tree.
  TreeNode* deserialize(string data) {
    if (data.empty()) {
      return nullptr;
    }
    vector<int> nums = split(data, ' ');
    int i = 0;
    function<TreeNode*(int, int)> dfs = [&](int mi, int mx) -> TreeNode* {
      if (i == nums.size() || nums[i] < mi || nums[i] > mx) {
        return nullptr;
      }
      int x = nums[i++];
      TreeNode* root = new TreeNode(x);
      root->left = dfs(mi, x);
      root->right = dfs(x, mx);
      return root;
    };
    return dfs(INT_MIN, INT_MAX);
  }

  vector<int> split(const string& s, char delim) {
    vector<int> tokens;
    stringstream ss(s);
    string token;
    while (getline(ss, token, delim)) {
      tokens.push_back(stoi(token));
    }
    return tokens;
  }
};

// Your Codec object will be instantiated and called as such:
// Codec* ser = new Codec();
// Codec* deser = new Codec();
// string tree = ser->serialize(root);
// TreeNode* ans = deser->deserialize(tree);
// return ans;
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */

type Codec struct {
}

func Constructor() Codec {
  return Codec{}
}

// Serializes a tree to a single string.
func (this *Codec) serialize(root *TreeNode) string {
  if root == nil {
    return ""
  }
  data := &strings.Builder{}
  var dfs func(*TreeNode)
  dfs = func(root *TreeNode) {
    if root == nil {
      return
    }
    data.WriteString(strconv.Itoa(root.Val))
    data.WriteByte(' ')
    dfs(root.Left)
    dfs(root.Right)
  }
  dfs(root)
  return data.String()[0 : data.Len()-1]
}

// Deserializes your encoded data to tree.
func (this *Codec) deserialize(data string) *TreeNode {
  if data == "" {
    return nil
  }
  vals := strings.Split(data, " ")
  i := 0
  var dfs func(int, int) *TreeNode
  dfs = func(mi, mx int) *TreeNode {
    if i == len(vals) {
      return nil
    }
    x, _ := strconv.Atoi(vals[i])
    if x < mi || x > mx {
      return nil
    }
    i++
    root := &TreeNode{Val: x}
    root.Left = dfs(mi, x)
    root.Right = dfs(x, mx)
    return root
  }
  return dfs(math.MinInt64, math.MaxInt64)
}

/**
 * Your Codec object will be instantiated and called as such:
 * ser := Constructor()
 * deser := Constructor()
 * tree := ser.serialize(root)
 * ans := deser.deserialize(tree)
 * return ans
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文