[leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树

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.

Example:

You may serialize the following tree:

    1
   /   2   3
     /     4   5

as "[1,2,3,null,null,4,5]"

Clarification: The above format is the same as how LeetCode 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 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Codec {
11
12     // Encodes a tree to a single string.
13     public String serialize(TreeNode root) {
14         StringBuilder sb = new StringBuilder();
15         buildString(root, sb);
16         return sb.toString();
17     }
18
19     private void buildString(TreeNode root, StringBuilder sb){
20         if(root == null){
21             // splitter: ","
22             sb.append("#").append(",");
23         }else{
24             // I use pre-order traversal: root, left, right
25             sb.append(root.val).append(",");
26             buildString(root.left, sb);
27             buildString(root.right, sb);
28         }
29     }
30
31     // Decodes your encoded data to tree.
32     public TreeNode deserialize(String data) {
33         if(data == null) return null;
34         String[]stringArr = data.split(",");
35         Queue<String> queue = new LinkedList<>();
36         // put each item of stringArr into queue
37         Collections.addAll(queue, stringArr);
38         return buildTree(queue);
39     }
40
41     private TreeNode buildTree(Queue<String> queue){
42         if(queue.isEmpty()) return null;
43         String curStr = queue.poll();
44         if(curStr.equals("#")) return null;
45         // to match serialize traversal order, I use pre-order traversal: root, left, right
46         TreeNode root = new TreeNode(Integer.parseInt(curStr));
47         root.left = buildTree(queue);
48         root.right = buildTree(queue);
49         return root;
50     }
51 }
52
53 // Your Codec object will be instantiated and called as such:
54 // Codec codec = new Codec();
55 // codec.deserialize(codec.serialize(root));

原文地址:https://www.cnblogs.com/liuliu5151/p/9808265.html

时间: 2024-08-02 08:38:04

[leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树的相关文章

[Lintcode]7. Serialize and Deserialize Binary Tree/[Leetcode]297. Serialize and Deserialize Binary Tree

7. Serialize and Deserialize Binary Tree/297. Serialize and Deserialize Binary Tree 本题难度: Medium/Hard Topic: Binary Tree Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called '

LeetCode 297: Serialize and Deserialize Binary Tree

1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Codec { 11 12 // Encodes a tree to a single string. 13 public Str

[leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树

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 comput

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree Total Accepted: 47713 Total Submissions: 149430 Difficulty: Hard Contributors: Admin Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in

[LeetCode][JavaScript]Serialize and Deserialize Binary Tree

Serialize and Deserialize Binary Tree 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 reconstruct

LeetCode OJ 297. Serialize and Deserialize Binary Tree

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 comput

【LeetCode】297. Serialize and Deserialize Binary Tree

二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流'ostringstream'. istringstream in; in >> str; 这里没执行一次就会倒出一个string (因为in流中使用了' '空格 作为分割符, 所以可以分成很多个string) 建树的时候使用先序建立二叉树. 关键代码如下: TreeNode* build(ist

难1 297. Serialize and Deserialize Binary Tree

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 compute

LeetCode——Serialize and Deserialize Binary Tree

Description: 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 a