首页 > 其他 > 详细

297. Serialize and Deserialize Binary Tree

时间:2015-12-15 07:43:01      阅读:285      评论:0      收藏:0      [点我收藏+]

题目:

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.

链接: http://leetcode.com/problems/serialize-and-deserialize-binary-tree/

题解:

序列化Binary Tree。 这里因为给定的Tree的val是Integer,所以我们可以用一个字符型的常量当做delimiter,比如‘#‘。然后我们可以使用两种方法, pre-order traversal,或者level-order traversal。两种方法的时间复杂度和空间复杂度都一样。下面是pre-order traversal的:

Time Complexity - O(n), Space Compleixty - O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {
    private final String delimiter = ",";
    private final String emptyNode = "#";
    
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {            
        StringBuilder sb = new StringBuilder();
        serialize(root, sb);
        return sb.toString();
    }
    
    private void serialize(TreeNode root, StringBuilder sb) {
        if(root == null) {
            sb.append(emptyNode).append(delimiter);
        } else {
            sb.append(root.val).append(delimiter);
            serialize(root.left, sb);
            serialize(root.right, sb);
        }
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        Deque<String> nodes = new LinkedList<>();
        nodes.addAll(Arrays.asList(data.split(delimiter)));
        return deserialize(nodes);
    }
    
    private TreeNode deserialize(Deque<String> nodes) {
        String nodeVal = nodes.pollFirst();
        if(nodeVal.equals(emptyNode)) {
            return null;
        } else {
            TreeNode node = new TreeNode(Integer.parseInt(nodeVal));
            node.left = deserialize(nodes);
            node.right = deserialize(nodes);
            return node;
        }
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

 

Reference:

http://articles.leetcode.com/2010/09/serializationdeserialization-of-binary.html

http://articles.leetcode.com/2010/09/saving-binary-search-tree-to-file.html

https://leetcode.com/discuss/66147/recursive-preorder-python-and-c-o-n

https://leetcode.com/discuss/66117/easy-to-understand-java-solution

https://leetcode.com/discuss/66698/java-bfs-based-accepted-solution-using-queue

https://leetcode.com/discuss/66181/easy-to-understand-java-solution

https://leetcode.com/discuss/69390/simple-solution-%23preorder-traversal-%23recursive-%23simple-logic

https://leetcode.com/discuss/66077/c-accepted-o-n-easy-solution

https://leetcode.com/discuss/66397/java-stack-based-solution-using-json

https://leetcode.com/discuss/66076/java-solution-with-queue

https://leetcode.com/discuss/66625/a-level-order-traversal-solution

https://leetcode.com/discuss/66141/a-48-ms-rough-c-solution-used-queue

 

297. Serialize and Deserialize Binary Tree

原文:http://www.cnblogs.com/yrbbest/p/5047035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!