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 String serialize(TreeNode root) {
14         if (root == null) {
15             return null;
16         }
17         StringBuilder result = new StringBuilder();
18         dfs(root, result);
19         return result.toString();
20     }
21
22     private void dfs(TreeNode root, StringBuilder sb) {
23         if (root == null) {
24             sb.append(" ").append("#");
25             return;
26         }
27         sb.append(root.val).append("#");
28         dfs(root.left, sb);
29         dfs(root.right, sb);
30     }
31
32     // Decodes your encoded data to tree.
33     public TreeNode deserialize(String data) {
34         if (data == null) {
35             return null;
36         }
37         LinkedList<String> nodeList = new LinkedList<>();
38         nodeList.addAll(Arrays.asList(data.split("#")));
39         return getTree(nodeList);
40     }
41
42     private TreeNode getTree(LinkedList<String> nodeList) {
43         String current = nodeList.poll();
44         if (current == null || current.equals(" ")) {
45             return null;
46         }
47
48         TreeNode root = new TreeNode(Integer.valueOf(current));
49         root.left = getTree(nodeList);
50         root.right = getTree(nodeList);
51         return root;
52     }
53 }
54
55 // Your Codec object will be instantiated and called as such:
56 // Codec codec = new Codec();
57 // codec.deserialize(codec.serialize(root));
时间: 2024-10-06 18:28:24

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 序列化与反序列化二叉树

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

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——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