LeetCode——Serialize and Deserialize Binary Tree
Description:
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 tree
1 / \ 2 3 / \ 4 5
as"[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.
本以为二叉树已经刷的比较溜了,但是还是踩了各种坑。。。
题目大意是按照对应的规则序列化和反序列化二叉树。其实就是求二叉树"按层"遍历序列和根据按层遍历序列求二叉树。但是这里的按层遍历是局部对称的,如果一个节点不为null,则就必须存储它的左右子节点,如果子节点为null就存成"null"。注意他的调用方式是先序列化再反序列化如果和原来的树相同的话就是正确的,所以中间返回的序列只要在反序列化时能解析就行,没有固定格式。
思路:序列化时按层遍历,用一个队列来保存当前节点。注意相对于普通的按层遍历,有一个条件不能少,就是在不能使用队列为null作为循环终止条件,因为最后一个叶子节点的左右子节点一定是null的,这样的话序列化的字符串就会多出来“null”,需要加一个判定有效节点个数的条件。如果非要使用队列为null作为唯一循环终止的条件的话就需要在反序列化时去掉序列最后所有的"null"。
AC代码:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Codec { 11 12 private int cnt; 13 // Encodes a tree to a single string. 14 public String serialize(TreeNode root) { 15 nodeCount(root); 16 StringBuilder res = new StringBuilder(); 17 Deque<TreeNode> queue = new LinkedList<TreeNode>(); 18 if(root == null) { 19 return null; 20 } 21 22 queue.offer(root); 23 int count = 0; 24 while(count < cnt && !queue.isEmpty()) { 25 26 TreeNode node = queue.peek(); 27 if(node == null) { 28 res.append("null "); 29 queue.poll(); 30 } 31 else { 32 res.append(node.val + " "); 33 count ++; 34 queue.poll(); 35 if(node.left != null) { 36 queue.offer(node.left); 37 } 38 else { 39 queue.offer(null); 40 } 41 if(node.right != null) { 42 queue.offer(node.right); 43 } 44 else { 45 queue.offer(null); 46 } 47 } 48 49 } 50 return res.toString(); 51 } 52 53 public int treeDepth(TreeNode root) { 54 int dep = 0; 55 if(root != null) { 56 int leftDp = treeDepth(root.left); 57 int rightDp = treeDepth(root.right); 58 dep = leftDp>=rightDp? leftDp+1:rightDp+1; 59 } 60 return dep; 61 } 62 63 64 public void nodeCount(TreeNode root) { 65 if(root != null) { 66 cnt ++; 67 nodeCount(root.left); 68 nodeCount(root.right); 69 } 70 71 } 72 73 // Decodes your encoded data to tree. 74 public TreeNode deserialize(String data) { 75 76 if(data == null) { 77 return null; 78 } 79 80 Deque<String> queue = new LinkedList<String>(); 81 Deque<TreeNode> nodeQueue = new LinkedList<TreeNode>(); 82 String[] spData = data.split(" "); 83 int i = spData.length - 1; 84 while(spData[i].equals("null")) i--; 85 for(int j=0; j<=i; j++) { 86 queue.offer(spData[j]); 87 } 88 89 TreeNode root = new TreeNode(Integer.parseInt(queue.poll())); 90 91 nodeQueue.offer(root); 92 93 while(!queue.isEmpty()) { 94 TreeNode p = nodeQueue.poll(); 95 String lc = queue.peek(); 96 if(!queue.isEmpty()) { 97 queue.poll(); 98 if(lc != null) { 99 if(!lc.equals("null")) { 100 TreeNode node = new TreeNode(Integer.parseInt(lc)); 101 p.left = node; 102 nodeQueue.offer(node); 103 } 104 } 105 106 } 107 108 if(!queue.isEmpty()) { 109 String rc = queue.peek(); 110 queue.poll(); 111 if(rc != null) { 112 if(!rc.equals("null")) { 113 TreeNode node = new TreeNode(Integer.parseInt(rc)); 114 p.right = node; 115 nodeQueue.offer(node); 116 } 117 } 118 } 119 } 120 return root; 121 } 122 } 123 124 // Your Codec object will be instantiated and called as such: 125 // Codec codec = new Codec(); 126 // codec.deserialize(codec.serialize(root));
![](https://images2015.cnblogs.com/blog/735119/201701/735119-20170111112835275-168981902.gif)
![](https://images2015.cnblogs.com/blog/735119/201701/735119-20170111112841431-2047172832.jpg)
![](https://images2015.cnblogs.com/blog/735119/201701/735119-20170111112847494-1544911856.jpg)
![](https://images2015.cnblogs.com/blog/735119/201701/735119-20170111112904385-918783221.jpg)
![](https://images2015.cnblogs.com/blog/735119/201701/735119-20170111120143119-1302805212.jpg)
![](https://images2015.cnblogs.com/blog/735119/201701/735119-20170111112856369-1466718103.jpg)
![](https://images2015.cnblogs.com/blog/735119/201701/735119-20170111112830416-794416355.jpg)
![](https://images2015.cnblogs.com/blog/735119/201701/735119-20170111113919510-1155220901.gif)