码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Serialize and Deserialize Binary Tree

时间:2015-11-04 07:03:55      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:

原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-binary-tree/

把一个树serialize 和 deserialize. 递归方法是通过pre-order来serialize.

    1
   /   2   3
     /     4   5

例子就变成了"1,2,#,#,3,4,#,#,5,#,#". 然后deserialize先读入一个字符作为根节点,然后根节点的左右子节点递归调用deserializeHelper函数.

这里学到了StringTokenizer: StringTokenizer st = new StringTokenizer(data, ",")可以把data 用"," 隔开生成StringTokenizer.

StringTokenizer 的两个method: st.hasMoreTokens() 返回是否还有token 和 st.nextToken() 返回下一个string. StringTokenizer 很像 string.split().

但是他可以在中间过程中处理分出来的字符串。

AC Java:

 1 public class Codec {
 2 
 3     // Encodes a tree to a single string.
 4     public String serialize(TreeNode root) {
 5         StringBuilder sb = new StringBuilder();
 6         serializeHelper(root,sb);
 7         return sb.toString();
 8     }
 9     
10     private void serializeHelper(TreeNode root, StringBuilder sb){
11         if(root == null){
12             sb.append("#,");
13             return;
14         }
15         sb.append(root.val).append(",");
16         serializeHelper(root.left, sb);
17         serializeHelper(root.right, sb);
18     }
19 
20     // Decodes your encoded data to tree.
21     public TreeNode deserialize(String data) {
22         StringTokenizer st = new StringTokenizer(data, ",");
23         return deserializeHelper(st);
24     }
25     
26     private TreeNode deserializeHelper(StringTokenizer st){
27         if(!st.hasMoreTokens()){
28             return null;
29         }
30         String cur = st.nextToken();
31         if(cur.equals("#")){
32             return null;
33         }
34         TreeNode root = new TreeNode(Integer.valueOf(cur));
35         root.left = deserializeHelper(st);
36         root.right = deserializeHelper(st);
37         return root;
38     }
39 }

 

Iteration 方法使用的BFS. 当处理serialize, queue不为空时,弹出节点cur, 若cur是null, 就在sb后面加上"#,"; 若不是null, 就在sb后面加上cur.val 和 ",".

处理deserialize时,先把string 用"," 格成string 数组。 如果是满树,那么左节点index就是2*i+1, 右节点index是2*i+2. 但这里不是满树。

需要同时记录当前i前面有几个null节点,用count存储,左节点index变成2*(i-count[i]) + 1, 右节点index变成2*(i-count[i]) + 2.

Note: 记住Queue 是 abstract class, 不能直接用来生成 que, 需要用LinkedList.

AC Java:

 1 public class Codec {
 2 
 3     // Encodes a tree to a single string.
 4     public String serialize(TreeNode root) {
 5         StringBuilder sb = new StringBuilder();
 6         Queue<TreeNode> que = new LinkedList<TreeNode>(); //error
 7         que.offer(root);
 8         while(!que.isEmpty()){
 9             TreeNode cur = que.poll();
10             if(cur == null){
11                 sb.append("#,");
12             }else{
13                 sb.append(cur.val).append(",");
14                 que.offer(cur.left);
15                 que.offer(cur.right);
16             }
17         }
18         return sb.toString();
19     }
20 
21     // Decodes your encoded data to tree.
22     public TreeNode deserialize(String data) {
23         String [] s = data.split(",");
24         int [] count = new int[s.length];
25         TreeNode [] tns = new TreeNode[s.length];
26         
27         for(int i = 0; i<s.length; i++){
28             if(i>0){
29                 count[i] = count[i-1];
30             }
31             if(s[i].equals("#")){
32                 tns[i] = null;
33                 count[i]++;
34             }else{
35                 tns[i] = new TreeNode(Integer.valueOf(s[i]));
36             }
37         }
38         
39         for(int i = 0; i<s.length; i++){
40             if(s[i].equals("#")){
41                 continue;
42             }
43             tns[i].left = tns[2 * (i-count[i]) + 1];
44             tns[i].right = tns[2 * (i-count[i]) + 2];
45         }
46         return tns[0];
47     }
48 }

 

参考了这篇帖子:http://blog.csdn.net/ljiabin/article/details/49474445

 

LeetCode Serialize and Deserialize Binary Tree

标签:

原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/4934908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!