Lintcode 7 Binary Tree Serialization

Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.

 Notice

There is no limit of how you deserialize or serialize a binary tree, LintCode will take your output of serialize as the input of deserialize, it won't check the result of serialize.

Example

An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:

  3
 / \
9  20
  /  \
 15   7

Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.

You can use other method to do serializaiton and deserialization.


算法:

M
  
 方法1: BFS. Non-recursive, using queue. 想法直观。level-order traversal. save到一个string里面就好。
  
 方法2: DFS. Recursive. 需要一点思考。basically divide and conquer. 但是代码相对来说短。
  
 ```
  
 /*
 Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.
 
 There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.
 
 Have you met this question in a real interview? Yes
 Example
 An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:
 
  3
  / \
 9 20
  / \
  15 7
 Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.
 
 You can use other method to do serializaiton and deserialization.
 
 Tags Expand
 Binary Tree Microsoft Yahoo
 
 */
  
  
 //BFS. store as string, separated by ','
 //Note: need to record null node as well. but be careful don't push null into queue
 class Solution {
 /**
  * This method will be invoked first, you should design your own algorithm
  * to serialize a binary tree which denote by a root node to a string which
  * can be easily deserialized by your own "deserialize" method later.
  */
 public String serialize(TreeNode root) {
 String rst = "";
 if (root == null) {
 return rst;
 }
 Queue<TreeNode> queue = new LinkedList<TreeNode>();
 queue.offer(root);
 int size = 0;
 while (!queue.isEmpty()) {
 size = queue.size();
 for (int i = 0; i < size; i++) {
 TreeNode node = queue.poll();
 if (node.val == Integer.MIN_VALUE) {
 rst += "#,";
 } else {
 rst += node.val + ",";
 TreeNode left = node.left == null ?
 new TreeNode(Integer.MIN_VALUE) : node.left;
 queue.offer(left);
 TreeNode right = node.right == null ?
 new TreeNode(Integer.MIN_VALUE) : node.right;
 queue.offer(right);
 }
 }
 }
 return rst;
 }
  
 /**
  * This method will be invoked second, the argument data is what exactly
  * you serialized at method "serialize", that means the data is not given by
  * system, it's given by your own serialize method. So the format of data is
  * designed by yourself, and deserialize it here as you serialize it in
  * "serialize" method.
  */
 public TreeNode deserialize(String data) {
 if (data == null || data.length() == 0) {
 return null;
 }
 TreeNode root = new TreeNode(0);
 root.val = Integer.parseInt(data.substring(0, data.indexOf(",")));
 data = data.substring(data.indexOf(",") + 1);
  
 Queue<TreeNode> queue = new LinkedList<TreeNode>();
 queue.offer(root);
 int size = 0;
 while (!queue.isEmpty()) {
 size = queue.size();
 for (int i = 0; i < size; i++) {
 TreeNode node = queue.poll();
 String temp = data.substring(0, data.indexOf(","));
 if (!temp.equals("#")) {
 node.left = new TreeNode(Integer.parseInt(temp));
 queue.offer(node.left);
 }
 data = data.substring(data.indexOf(",") + 1);
  
 temp = data.substring(0, data.indexOf(","));
 if (!temp.equals("#")) {
 node.right = new TreeNode(Integer.parseInt(temp));
 queue.offer(node.right);
 }
 data = data.substring(data.indexOf(",") + 1);
 }
 }
  
 return root;
 }
 }
  
  
  
  
  
  
  
  
  
  
  
  
 //DFS approach, recursive
  
 /**
  * Definition of TreeNode:
  * public class TreeNode {
  * public int val;
  * public TreeNode left, right;
  * public TreeNode(int val) {
  * this.val = val;
  * this.left = this.right = null;
  * }
  * }
  */
 class Solution {
 /**
  * This method will be invoked first, you should design your own algorithm
  * to serialize a binary tree which denote by a root node to a string which
  * can be easily deserialized by your own "deserialize" method later.
  */
 public String serialize(TreeNode root) {
 if (root == null) {
 return "#,";
 }
 String mid = root.val + ",";
 String left = serialize(root.left);
 String right = serialize(root.right);
 mid += left + right;
 return mid;
 }
  
 private String data = "";
 /**
  * This method will be invoked second, the argument data is what exactly
  * you serialized at method "serialize", that means the data is not given by
  * system, it's given by your own serialize method. So the format of data is
  * designed by yourself, and deserialize it here as you serialize it in
  * "serialize" method.
  */
 public TreeNode deserialize(String data) {
 this.data = data;
 return desHelper();
 }
  
 public TreeNode desHelper() {
 if (this.data.indexOf("#,") == 0) {
 this.data = this.data.substring(this.data.indexOf(",") + 1);
 return null;
 }
 String midVal = this.data.substring(0, this.data.indexOf(","));
 TreeNode mid = new TreeNode(Integer.parseInt(midVal));
 this.data = this.data.substring(this.data.indexOf(",") + 1);
 TreeNode left = desHelper();
 TreeNode right = desHelper();
 mid.left = left;
 mid.right = right;
 return mid;
 }
 }
  
 

class Solution {
    /**
     * This method will be invoked first, you should design your own algorithm 
     * to serialize a binary tree which denote by a root node to a string which
     * can be easily deserialized by your own "deserialize" method later.
     */
    public String serialize(TreeNode root) {
        if (root == null) {
            return "{}";
        }

        ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
        queue.add(root);

        for (int i = 0; i < queue.size(); i++) {
            TreeNode node = queue.get(i);
            if (node == null) {
                continue;
            }
            queue.add(node.left);
            queue.add(node.right);
        }

        while (queue.get(queue.size() - 1) == null) {
            queue.remove(queue.size() - 1);
        }

        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(queue.get(0).val);
        for (int i = 1; i < queue.size(); i++) {
            if (queue.get(i) == null) {
                sb.append(",#");
            } else {
                sb.append(",");
                sb.append(queue.get(i).val);
            }
        }
        sb.append("}");
        return sb.toString();
    }
    
    /**
     * This method will be invoked second, the argument data is what exactly
     * you serialized at method "serialize", that means the data is not given by
     * system, it's given by your own serialize method. So the format of data is
     * designed by yourself, and deserialize it here as you serialize it in 
     * "serialize" method.
     */
    public TreeNode deserialize(String data) {
        if (data.equals("{}")) {
            return null;
        }
        String[] vals = data.substring(1, data.length() - 1).split(",");
        ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
        TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
        queue.add(root);
        int index = 0;
        boolean isLeftChild = true;
        for (int i = 1; i < vals.length; i++) {
            if (!vals[i].equals("#")) {
                TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
                if (isLeftChild) {
                    queue.get(index).left = node;
                } else {
                    queue.get(index).right = node;
                }
                queue.add(node);
            }
            if (!isLeftChild) {
                index++;
            }
            isLeftChild = !isLeftChild;
        }
        return root;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值