题解 | #序列化二叉树#
序列化二叉树
https://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84
import java.util.*; /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { String Serialize(TreeNode root) { if (root == null) return "{}"; //为空返回{} StringJoiner sj = new StringJoiner(",", "{", "}"); Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { TreeNode node = queue.poll(); sj.add(node == null ? "#" : String.valueOf(node.val)); if (node != null) { queue.add(node.left); queue.add(node.right); } } String res = sj.toString(); while (res.endsWith("#}")) { res = res.substring(0, res.length() - 3) + "}"; } return res; } TreeNode Deserialize(String str) { if (str.equals("{}")) return null; String[] strs = str.substring(1, str.length() - 1).split(","); Queue<TreeNode> queue = new LinkedList<>(); int i = 0; TreeNode root = new TreeNode(Integer.parseInt(strs[i++])); queue.add(root); while (!queue.isEmpty() && i < strs.length) { TreeNode node = queue.poll(); if (node == null) continue; TreeNode leftNode = strs[i].equals("#") ? null : new TreeNode(Integer.parseInt( strs[i])); i++; node.left = leftNode; queue.add(leftNode); if (i >= strs.length) break; TreeNode rightNode = strs[i].equals("#") ? null : new TreeNode(Integer.parseInt( strs[i])); i++; node.right = rightNode; queue.add(rightNode); } return root; } }