摘要:题目大意将二叉树序列化,返回序列化的,和反序列化还原。解题思路技巧在于将记录为便于将来判断。的思想将每一层记录下来,反序列化时也按照层级遍历的方法依次设置为上一个里面的元素的左孩子和右孩子。变种,可以要求输出一个,而不是
LeetCode 297. Serialize and Deserialize Binary Tree
题目大意: 将二叉树序列化,返回序列化的String,和反序列化还原。
解题思路:
技巧在于将null记录为#便于将来判断。有两种解法。
Level Order Traversal - BFS的思想将每一层记录下来,反序列化时也按照层级遍历的方法依次设置为上一个queue里面的元素的左孩子和右孩子。
更好的方法为preorder traversal,是可以handle变种题目的解法,利用preorder是root—>left->right的顺序,用一个deque来不断把头部的元素poll出,递归调用函数构建还原二叉树。
//Solution 1: using Level order traversal public static String serialize(TreeNode root) { if (root == null ) { return ""; } StringBuilder sb = new StringBuilder(); Queueq = new LinkedList (); q.offer(root); while(!q.isEmpty()) { TreeNode curr = q.poll(); if (curr == null) { sb.append("#,"); } else { sb.append(curr.val + ","); q.offer(curr.left); q.offer(curr.right); } } sb.deleteCharAt(sb.length() - 1); return sb.toString(); } /** * 1 * / * 2 3 * / / * 4 2 4 * / * 4 * [1,2,3,4,#,2,4,4] * @param input * @return */ public static TreeNode deSerialize(String input) { if (input == null || input.length() == 0) { return null; } String[] strs = input.split(","); TreeNode root = new TreeNode(Integer.valueOf(strs[0])); Queue q = new LinkedList (); q.offer(root); for(int i = 1; i < strs.length; i++){ TreeNode curr = q.poll(); if (curr == null) continue; if (!strs[i].equals("#")) { curr.left = new TreeNode(Integer.valueOf(strs[i])); q.offer(curr.left); } i++; if (i < strs.length && !strs[i].equals("#")) { curr.right = new TreeNode(Integer.valueOf(strs[i])); q.offer(curr.right); } } return root; } //Solution II: using 2 preorder traversal private static final String DELIMITER = ","; private static final String NN = "#"; // Encodes a tree to a single string. public static String serialize2(TreeNode root) { //using preorder traversal StringBuilder res = new StringBuilder(); serializeHelper(root, res); res.deleteCharAt(res.length() - 1); return res.toString(); } private static void serializeHelper(TreeNode root, StringBuilder res) { if (root == null) { res.append(NN).append(DELIMITER); return; } res.append(root.val).append(DELIMITER); serializeHelper(root.left, res); serializeHelper(root.right, res); } // Decodes your encoded data to tree. public static TreeNode deserialize2(String data) { Deque deque = new LinkedList<>(); deque.addAll(Arrays.asList(data.split(DELIMITER))); return deserializeHelper(deque); } private static TreeNode deserializeHelper(Deque deque) { String now = deque.pollFirst(); if (now.equals(NN)) { return null; } TreeNode node = new TreeNode(Integer.parseInt(now)); node.left = deserializeHelper(deque); node.right = deserializeHelper(deque); return node; } public static void main(String[] args) { //solution 1 level order TreeNode root1 = deSerialize("1,2,3,4,#,5,6,7"); String result1 = serialize(root1); System.out.println(result1); //solution 2 preorder - 变种,可以要求输出一个LinkedList,而不是String TreeNode root2 = deserialize2("3,4,1,#,#,2,#,#,5,#,6,#,#"); String result2 = serialize2(root2); System.out.println(result2); }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71807.html
Problem 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 li...
摘要:因此题目中的例子的中序遍历结果为。但是,标准的中序遍历并不能使我们将该结果反构造成一棵树,我们丢失了父节点和子节点之间的关系。这里我们也可以明显的看出来,中序遍历需要保存的空值远远多于广度优先遍历。 题目要求 Serialization is the process of converting a data structure or object into a sequence of ...
摘要:题目解答用一个特殊的符号来表示的情况这是按先序遍历来存到中去这里用为其包含了几乎所有这里还是挺多知识点的,后是一个可以把数组变成则是把加到这个的中去 题目:Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stor...
摘要:思路理论上说所有遍历的方法都可以。但是为了使和的过程都尽量最简单,是不错的选择。用作为分隔符,来表示。复杂度代码思路这道题和之前不同,一般的树变成了,而且要求是。还是可以用,还是需要分隔符,但是就不需要保存了。 297. Serialize and Deserialize Binary Tree Serialization is the process of converting a...
摘要:题目要求将二叉搜索树序列化和反序列化,序列化是指将树用字符串的形式表示,反序列化是指将字符串形式的树还原成原来的样子。假如二叉搜索树的节点较多,该算法将会占用大量的额外空间。 题目要求 Serialization is the process of converting a data structure or object into a sequence of bits so that...
阅读 961·2021-11-24 09:39
阅读 2119·2021-11-16 11:54
阅读 2023·2021-11-11 17:22
阅读 2349·2021-09-30 09:55
阅读 3547·2021-08-12 13:22
阅读 1601·2019-08-30 15:44
阅读 1151·2019-08-29 12:12
阅读 3245·2019-08-27 10:58