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’.
Approach 1.
========
Convert the given BST into a doubly linked list in O(n) and then send the linked list over the channel.
Note:: See Blog Convert tree into linked list in O(n) using tree rotations.
Once you recieve the tree then again convert back the linked list to tree. I haven't done this yet in O(n); however there is a post avaiabe; if you have a solution for second case please do post.
Approach 2
========
1. Take the preorder traversal of the tree; write it to a file and send it over the channel. This would be done using O(n).
Since you have to traverse the tree to save the nodes.
Possible traversals are inorder, preorder, postorder and level order.
If we do an inorder traversal then we would get a sorted list which if we convert into a BST would again become a list and we would loose out on the original structure of the tree.
Postorder traversal would also not give back the original tree; a simple example can let you know.
Now left out are preorder and level order traversal.
Both give us the output; but level order will require more space as it uses BFS approach. So we do a preorder traversal of the tree store it in a file in O(n) and send it over the channel.
On the recieving end we recieve and construct the tree back in O(n log(n)); by inserting each and every node in the preorder list into the new list using the property of BST results the same tree....
class TreeNode{ int val; TreeNode left, right; TreeNode(int val){ this.val = val; } } public String serialize(TreeNode root){ StringBuilder sb = new StringBuilder(); serialize(root, sb); return sb.toString(); } private void serialize(TreeNode x, StringBuilder sb){ if (x == null) { sb.append("# "); } else { sb.append(x.val + " "); serialzie(x.left, sb); serialzie(x.right, sb); } } public TreeNode deserialize(String s){ if (s == null || s.length() == 0) return null; StringTokenizer st = new StringTokenizer(s, " "); return deserialize(st); } private TreeNode deserialize(StringTokenizer st){ if (!st.hasMoreTokens()) return null; String val = st.nextToken(); if (val.equals("#")) return null; TreeNode root = new TreeNode(Integer.parseInt(val)); root.left = deserialize(st); root.right = deserialize(st); return root; }
References:
http://www.geeksforgeeks.org/serialize-deserialize-binary-tree/
http://www.cs.usfca.edu/~galles/cs245/lecture/lecture9.pdf
相关推荐
c c语言_leetcode题解之0297_serialize_and_deserialize_binary_tree
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
lru cache leetcode leetcode 记录自己刷leetcode时遇到的一些值得记下来的...binary-tree-preorder-traversal n-queens-ii populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best-time-to-buy
Tree](Basic-Knowledge/Binary tree.md) Serialize And Deserialize Bit 1.position 2.sequence Sell Stock 区间型 矩阵坐标 Word Ladder Add Two Numbers Matrix Spiral Matrix Mergesort [Algorithm Swap]...
leetcode添加元素使和等于 经验教训 一定要吧功能尽量细化为函数,这样一者做题思路比较清晰,二者可以在某些情况下直接return值。 如果输入的形式是一个序列,则可以想想:分治、动规、贪婪,一般不建议搜索,因为...
- “Serialize and Deserialize Binary Tree(序列化和反序列化二叉树)”考察二叉树的理解和实现。 - “Knight Dialer(骑士拨号器)”结合数学和编程解决现实问题。 - “Add Two Numbers(两数相加)”是链表...
Serialize and Deserialize Binary Tree、298. Binary Tree Longest Consecutive Sequence等。这类题目通常要求读者使用树的各种操作来解决问题,例如树的遍历、查找、插入等操作。 4. 图类题目 图类题目是...