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 treeapp1 / \ 2 3 / \ 4 5as "[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.less
理論上說全部遍歷的方法均可以。可是爲了使serialize和deserialize的過程都儘可能最簡單,preorder是不錯的選擇。serialize的話,dfs比較好寫,deserialize的話preorder和bfs比較好寫。用「,」做爲分隔符,「#」來表示null。例子裏serialize以後結果就變成"1,2,3,#,#,4,5"。deserialize的時候用一個queue來保存string。ui
Time: O(N), Space: O(N)this
// Encodes a tree to a single string. public String serialize(TreeNode root) { // base case if(root == null) return ""; StringBuilder encoded = new StringBuilder(); encode(root, encoded); return encoded.substring(1).toString(); } private void encode(TreeNode root, StringBuilder sb) { if(root == null) { sb.append(",#"); return; } sb.append(",").append(root.val); encode(root.left, sb); encode(root.right, sb); } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { // base case if(data.length() == 0) return null; Queue<String> q = new LinkedList(Arrays.asList(data.split(","))); return decode(q); } private TreeNode decode(Queue<String> q) { if(q.isEmpty()) return null; String cur = q.poll(); if(cur.equals("#")) return null; TreeNode root = new TreeNode(Integer.valueOf(cur)); root.left = decode(q); root.right = decode(q); return root; }
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.rest
The encoded string should be as compact as possible.code
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.orm
這道題和以前不一樣,通常的樹變成了BST,並且要求是as compact as possible。仍是能夠用preorder,仍是須要分隔符,可是null就不須要保存了。deserialize部分要變得複雜,left的值老是小於root的值,right的值老是大於root的值,根據這個每次recursion的時候把左邊的值都放到另外一個queue裏面,剩下的就是右邊的值。string
Time: O(N^2), Space: O(N)it
// Encodes a tree to a single string. public String serialize(TreeNode root) { // base case if(root == null) return ""; StringBuilder encoded = new StringBuilder(); encode(root, encoded); return encoded.substring(1).toString(); } private void encode(TreeNode root, StringBuilder sb) { if(root == null) return; sb.append(",").append(root.val); encode(root.left, sb); encode(root.right, sb); } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { // base case if(data.length() == 0) return null; Queue<Integer> q = new LinkedList(); for(String s : data.split(",")) q.offer(Integer.valueOf(s)); return decode(q); } private TreeNode decode(Queue<Integer> q) { if(q.isEmpty()) return null; int cur = q.poll(); TreeNode root = new TreeNode(cur); Queue<Integer> left = new LinkedList(); while(!q.isEmpty() && q.peek() < cur) left.offer(q.poll()); root.left = decode(left); root.right = decode(q); return root; }