297:二叉树的序列化与反序列化
This commit is contained in:
parent
4eaa41837e
commit
743ee22cbc
@ -0,0 +1,121 @@
|
||||
//序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方
|
||||
//式重构得到原数据。
|
||||
//
|
||||
// 请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串
|
||||
//反序列化为原始的树结构。
|
||||
//
|
||||
// 提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的
|
||||
//方法解决这个问题。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:root = [1,2,3,null,null,4,5]
|
||||
//输出:[1,2,3,null,null,4,5]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:root = []
|
||||
//输出:[]
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:root = [1]
|
||||
//输出:[1]
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:root = [1,2]
|
||||
//输出:[1,2]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 树中结点数在范围 [0, 104] 内
|
||||
// -1000 <= Node.val <= 1000
|
||||
//
|
||||
// Related Topics 树 深度优先搜索 广度优先搜索 设计 字符串 二叉树
|
||||
// 👍 586 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
//297:二叉树的序列化与反序列化
|
||||
public class SerializeAndDeserializeBinaryTree {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
// Solution solution = new SerializeAndDeserializeBinaryTree().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
public class Codec {
|
||||
|
||||
// Encodes a tree to a single string.
|
||||
public String serialize(TreeNode root) {
|
||||
String result = "";
|
||||
if (root == null) {
|
||||
return "null,";
|
||||
}
|
||||
result += root.val + ",";
|
||||
result += serialize(root.left);
|
||||
result += serialize(root.right);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Decodes your encoded data to tree.
|
||||
public TreeNode deserialize(String data) {
|
||||
if ("null,".equals(data)) {
|
||||
return null;
|
||||
}
|
||||
String[] strs = data.split(",");
|
||||
Queue<String> queue = new LinkedList<>();
|
||||
for (String str : strs) {
|
||||
queue.offer(str);
|
||||
}
|
||||
return des(queue);
|
||||
}
|
||||
|
||||
private TreeNode des(Queue<String> queue) {
|
||||
String value = queue.poll();
|
||||
if ("null".equals(value)) {
|
||||
return null;
|
||||
}
|
||||
TreeNode root = new TreeNode(Integer.parseInt(value));
|
||||
root.left = des(queue);
|
||||
root.right = des(queue);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
||||
// Your Codec object will be instantiated and called as such:
|
||||
// Codec ser = new Codec();
|
||||
// Codec deser = new Codec();
|
||||
// TreeNode ans = deser.deserialize(ser.serialize(root));
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<p>序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。</p>
|
||||
|
||||
<p>请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。</p>
|
||||
|
||||
<p><strong>提示: </strong>输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 <a href="/faq/#binary-tree">LeetCode 序列化二叉树的格式</a>。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg" style="width: 442px; height: 324px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,null,null,4,5]
|
||||
<strong>输出:</strong>[1,2,3,null,null,4,5]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = []
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1]
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2]
|
||||
<strong>输出:</strong>[1,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中结点数在范围 <code>[0, 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>-1000 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>设计</li><li>字符串</li><li>二叉树</li></div></div>\n<div><li>👍 586</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user