[LeetCode 297] Serialize and Deserialize Binary Tree

原创 2015年11月18日 16:28:34



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.

For example, you may serialize the following tree

    1
   / \
  2   3
     / \
    4   5
as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ 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.


solution:

1. preorder traversal, use another helper class(StringToken) to split string, and record current position

2. level order traversal


public class Codec {

        
 // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        helptoserialize(root, sb);
        return sb.toString();
    }

    public void helptoserialize(TreeNode root, StringBuilder sb) {
        if(root == null) {
            sb.append("# ");
        } else {
            sb.append(root.val);
            sb.append(" ");
            helptoserialize(root.left, sb);
            helptoserialize(root.right, sb);
        }
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        TreeNode root = helptodeserialize(new StringToken(data, " "));
        return root;
    }
    
    public TreeNode helptodeserialize(StringToken token) {
        if(!token.hasMoreElement()) return null;
        String t1 = token.nextElement();
        if(t1.equals("#")) return null;
        TreeNode root = new TreeNode(Integer.valueOf(t1));
        root.left = helptodeserialize(token);
        root.right = helptodeserialize(token);
        return root;
    }

    class StringToken {
        public int len = 0;
        ArrayList<String> dataStrings = new ArrayList<>();
        public StringToken (String data, String spliter) {
            String[] datas = data.split(spliter);
            for(int i=0;i<datas.length;i++) {
                dataStrings.add(datas[i]);
            }
        }
        public boolean hasMoreElement() {
            return len<dataStrings.size();
        }
        public String nextElement() {
            return dataStrings.get(len++);
        }
    }
}


相关文章推荐

[leetcode-297]Serialize and Deserialize Binary Tree(java)

问题描述:这里写链接内容 这道题思路毕竟简单,就是定义一套规则,一套序列化成字符串,然后再反序列化的规则。我的思路很简单:序列化过程中,采用广搜的思路,在对每一层而言,如果该节点为null,那么在字...
  • zdavb
  • zdavb
  • 2015年10月30日 21:27
  • 980

Leetcode 297 Serialize and Deserialize Binary Tree(序列化与反序列化二叉树)

1. 描述  设计一个算法,实现二叉树的序列化与反序列化。如何实现没有限制,只要保证一颗二叉树可以序列化为一个string串,然后这个string串可以反序列化为原来的二叉树即可。详细描述如下:  ...

Leetcode 297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree Total Accepted: 32395 Total Submissions: 107313 Diff...

日常小结-层序遍历的实现leetcode 297

层序遍历的实现leetcode 297-Serialize and Deserialize Binary Tree最近在写leetcode的测试集,目前想实现一个输入string类型的数组得到对应树的...

【LeetCode】Serialize and Deserialize Binary Tree 解题报告

【题目】 Serialization is the process of converting a data structure or object into a sequence of b...
  • ljiabin
  • ljiabin
  • 2015年10月28日 21:38
  • 9576

LeetCode 297. Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)

原题网址:https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the pro...
  • jmspan
  • jmspan
  • 2016年04月16日 15:44
  • 660

LeetCode 297. Serialize and Deserialize Binary Tree

题目LeetCode 297. Serialize and Deserialize Binary Tree仿照 331. Verify Preorder Serialization of a Bin...

LeetCode 297 Serialize and Deserialize Binary Tree

题目描述Serialization is the process of converting a data structure or object into a sequence of bits so...

景岁的Leetcode解题报告:297. Serialize and Deserialize Binary Tree(Python)

一种基于前序遍历的解法。

[leetcode] 297. Serialize and Deserialize Binary Tree 解题报告

题目链接:https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the pro...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode 297] Serialize and Deserialize Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)