博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binary Tree Serialisation Lintcode
阅读量:4629 次
发布时间:2019-06-09

本文共 6109 字,大约阅读时间需要 20 分钟。

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.

 

For this question I used recursion and bfs method to serialize and deserialize. It is not perfect but it works... I don't know... At least accepted by Lintcode...

/** * 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 {    int index = 0;    /**     * 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 null;        }        StringBuilder str = new StringBuilder();        helper(root, str);        return str.toString();    }        private void helper(TreeNode root, StringBuilder str) {        if (root == null) {            str.append("#");            str.append(",");            return;        }        str.append(root.val);        str.append(",");        helper(root.left, str);        helper(root.right, str);    }        /**     * 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;        }        String[] s = data.split(",");        if (s.length == 0) {            return null;        }        TreeNode root = buildTree(s);        return root;            }    private TreeNode buildTree(String[] s) {        if (index > s.length || s[index].equals("#")) {            index = index + 1;            return null;        }        TreeNode root = new TreeNode(Integer.valueOf(s[index]));        index = index + 1;        root.left = buildTree(s);        root.right = buildTree(s);        return root;    }}

Then I checked the answer in Jiuzhang, but they used different methods, which is not recursion. Their format is better than mine.

/** * 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 {    int index = 0;    /**     * 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
queue = new ArrayList<>(); queue.add(root); for (int i = 0; i < queue.size(); i++) { TreeNode n = queue.get(i); if (n == null) { continue; } queue.add(n.left); queue.add(n.right); } while (queue.get(queue.size() - 1) == null) { queue.remove(queue.size() - 1); } StringBuilder str = new StringBuilder(); str.append("{"); str.append(queue.get(0).val); for (int i = 1; i < queue.size(); i++) { if (queue.get(i) == null) { str.append(",#"); } else { str.append(","); str.append(queue.get(i).val); } } str.append("}"); return str.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[] s = data.substring(1, data.length() - 1).split(","); ArrayList
queue = new ArrayList<>(); TreeNode node = new TreeNode(Integer.valueOf(s[0])); queue.add(node); int index = 0; boolean isLeft = true; for (int i = 1; i < s.length; i++) { if (!s[i].equals("#")) { TreeNode t = queue.get(index); TreeNode tmp = new TreeNode(Integer.valueOf(s[i])); if (isLeft) { t.left = tmp; } else { t.right = tmp; } queue.add(tmp); } if (!isLeft) { index++; } isLeft = !isLeft; } return node; }}

The idea here when serializing is to put nodes in an arraylist layer by layer. When deserializing it is similar.

Notice:

When diserializing, it is smart to use a boolean to check it is left child or right child. And after a left and a right, the index should point to next node.

 

Non-recursion method is not very familiar to me. 还是回顾下吧。。。

转载于:https://www.cnblogs.com/aprilyang/p/6507814.html

你可能感兴趣的文章
单相计量芯片RN8209D使用经验分享(转)
查看>>
SD卡的控制方法(指令集和控制时序)
查看>>
zabbix4.0构建实录
查看>>
javascript保留字
查看>>
assert
查看>>
openstack安装在虚拟机上重启之后无法启动问题
查看>>
Bulk_Collect_Performance 比较
查看>>
类于对象
查看>>
灵活性是原则性基础上的灵活
查看>>
python 添加进度条
查看>>
恢复Opera11.50地址栏的下拉列表按钮
查看>>
EBS上用过的一些接口表整理信息
查看>>
ldconfig
查看>>
操作系统简介
查看>>
查看Linux系统中某目录的大小
查看>>
Git远程仓库地址变更
查看>>
PAT_B_1027 打印沙漏
查看>>
POJ-1185 炮兵阵地 动态规划+状态压缩
查看>>
NYOJ 366 D的小L
查看>>
PYTHON 写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者...
查看>>