二叉树序列化
297. Serialize and Deserialize Binary Tree
思路:preorder遍历 便于deserialize。时空O(N)。
public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { return doSerialize(root, ""); } private String doSerialize(TreeNode root, String str){ if(root == null){ str += "null,"; }else{ str += root.val + ","; str = doSerialize(root.left,str); str = doSerialize(root.right,str); } return str; } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { String[] nodeArray = data.split(","); List<String> nodeList = new LinkedList<>(Arrays.asList(nodeArray)); return doDeserialize(nodeList); } private TreeNode doDeserialize(List<String> list){ if(list.get(0).equals("null")){ list.remove(0); return null; } TreeNode root = new TreeNode(Integer.valueOf(list.get(0))); list.remove(0); root.left = doDeserialize(list); root.right = doDeserialize(list); return root; } }
449. Serialize and Deserialize BST
利用二叉树的性质构造子树,设置一个[low, high] 的range。此方法没有存储null元素,所以在利用queue的时候要先peek下一个元素,如果不在当前node的值范围内,就可知道当前node子节点为null。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { StringBuilder sb = new StringBuilder(); doSerialize(root, sb); return sb.toString(); } private void doSerialize(TreeNode root, StringBuilder sb){ if(root == null){ return; } sb.append(root.val).append(","); doSerialize(root.left, sb); doSerialize(root.right, sb); } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { if(data.isEmpty()){ return null; } String[] nodes = data.split(","); Queue<String> queue = new LinkedList<>(Arrays.asList(nodes)); return doDeserialize(queue, Integer.MIN_VALUE, Integer.MAX_VALUE); } private TreeNode doDeserialize(Queue<String> queue, int low, int high){ if(queue.isEmpty()){ return null; } int next = Integer.valueOf(queue.peek()); if(next<low || next>high){ return null; } TreeNode node = new TreeNode(Integer.valueOf(queue.remove())); node.left = doDeserialize(queue,low,node.val); node.right= doDeserialize(queue,node.val,high); return node; } } // Your Codec object will be instantiated and called as such: // Codec ser = new Codec(); // Codec deser = new Codec(); // String tree = ser.serialize(root); // TreeNode ans = deser.deserialize(tree); // return ans;
普通二叉树的序列化和反序列化:
先序遍历,null节点用特殊符号标记。
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner; public class Solution { public static void serialize(TreeNode root, PrintStream ps) { if (root == null) ps.print("# "); else { ps.print(root.val + " "); serialize(root.left, ps); serialize(root.right, ps); } } public static TreeNode deserialize(Scanner cin) { String token = cin.next(); if (token.equals("#")) return null; int val = Integer.parseInt(token); TreeNode root = new TreeNode(val); root.left = deserialize(cin); root.right = deserialize(cin); return root; } public static void main(String[] args) throws FileNotFoundException { TreeNode root = new TreeNode(30); root.left = new TreeNode(20); root.left.left = new TreeNode(10); root.right = new TreeNode(40); root.right.left = new TreeNode(35); root.right.right = new TreeNode(50); PrintStream ps = new PrintStream(new File("serialize.txt")); serialize(root, ps); Scanner cin = new Scanner(new File("serialize.txt")); TreeNode back = deserialize(cin); System.out.println(back.val); } }
BST的序列化和反序列化:
序列化:直接先序遍历输出
反序列化:根据先序遍历构造。O(n)时间。
import java.io.FileNotFoundException; import java.util.Scanner; public class Solution { private static int curVal; public static TreeNode deserializeBSTfromInorder(Scanner cin) { if (!cin.hasNext()) return null; else { curVal = cin.nextInt(); return deserialize(cin, Integer.MIN_VALUE, Integer.MAX_VALUE); } } private static TreeNode deserialize(Scanner cin, int min, int max) { if (curVal > min && curVal < max) { int val = curVal; TreeNode root = new TreeNode(val); if (cin.hasNext()) { curVal = cin.nextInt(); root.left = deserialize(cin, min, val); root.right = deserialize(cin, val, max); } return root; } else return null; } public static void main(String[] args) throws FileNotFoundException { String s = "30 20 10 40 35 50"; Scanner cin = new Scanner(s); TreeNode back = deserializeBSTfromInorder(cin); System.out.println(back.val); } }
从数组度数据:
public class Solution { private int curr; public TreeNode deserialize(int[] preorder) { curr = 0; return deserialize(preorder, Integer.MIN_VALUE, Integer.MAX_VALUE); } public TreeNode deserialize(int[] preorder, int min, int max) { if (curr >= preorder.length || preorder[curr] <= min || preorder[curr] >= max) { return null; } int val = preorder[curr++]; TreeNode root = new TreeNode(val); root.left = deserialize(preorder, min, val); root.right = deserialize(preorder, val, max); return root; } public static void main(String[] args) { int[] pre = new int[] { 30, 20, 10, 40, 35, 50 }; TreeNode root = new Solution().deserialize(pre); System.out.println(root); } }
参考:
http://leetcode.com/2010/09/saving-binary-search-tree-to-file.html
http://leetcode.com/2010/09/serializationdeserialization-of-binary.html