《剑指offer》第61题 序列化二叉树
序列化二叉树
https://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84?tpId=13&&tqId=11214&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述
请实现两个函数,分别用来序列化和反序列化二叉树
二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于先序、中序、后序、层序的二叉树遍历方式来进行修改,序列化的结果是一个字符串,序列化时通过 某种符号表示空节点(#),以 ! 表示一个结点值的结束(value!)。
二叉树的反序列化是指:根据某种遍历顺序得到的序列化字符串结果str,重构二叉树。
例如,我们可以把一个只有根节点为1的二叉树序列化为"1,",然后通过自己的函数来解析回这个二叉树
解题思路
思路一:中序遍历
利用队列来进行操作
代码如下:
import java.util.*; //层序遍历 public class Solution { String Serialize(TreeNode root){ StringBuffer sb=new StringBuffer(); Queue<TreeNode> queue=new LinkedList<TreeNode>(); if(root !=null) queue.add(root); while(!queue.isEmpty()){ TreeNode node=queue.poll(); if(node !=null){ queue.offer(node.left); queue.offer(node.right); sb.append(node.val+","); }else{ sb.append("#"+","); } } if(sb.length() != 0) sb.deleteCharAt(sb.length()-1); return sb.toString(); } //针对中序进行反序列化 TreeNode Deserialize(String str){ TreeNode head =null; if(str == null || str.length()==0) return head; String[] nodes = str.split(","); TreeNode[] treeNodes=new TreeNode[nodes.length]; for(int i=0;i<nodes.length;i++){ if(!nodes[i].equals("#")) treeNodes[i]=new TreeNode(Integer.valueOf(nodes[i])); } for(int i=0,j=1;j<treeNodes.length;i++){ if(treeNodes[i] != null){ treeNodes[i].left=treeNodes[j++]; treeNodes[i].right=treeNodes[j++]; } } return treeNodes[0]; } }
思路二:前序遍历
利用栈来进行操作
代码如下
public class Solution { String Serialize(TreeNode root) { StringBuilder sb = new StringBuilder(); getSerializeString(root, sb); if(sb.length() != 0) sb.deleteCharAt(sb.length()-1); return sb.toString(); } getSerializeString(TreeNode root, StringBuilder sb){ if(root == null) sb.append("#,"); else{ sb.append(root.val + ","); getSerializeString(root.left, sb); getSerializeString(root.right, sb); } } TreeNode Deserialize(String str) { if(str == null || str.length() == 0 || str.length() ==1) return null; String[] nodes = str.split(","); TreeNode[] treeNodes = new TreeNode[nodes.length]; for(int i=0; i<nodes.length; i++){ if(!nodes[i].equals("#")) treeNodes[i] = new TreeNode(Integer.valueOf(nodes[i])); } Stack<TreeNode> stack = new Stack<>(); stack.push(treeNodes[0]); int i = 1; while(treeNodes[i] != null){ stack.peek().left = treeNodes[i]; stack.push(treeNodes[i++]); } while(!stack.isEmpty()){ stack.pop().right = treeNodes[++i]; if(treeNodes[i] != null){ stack.push(treeNodes[i++]); while(treeNodes[i] != null){ stack.peek().left = treeNodes[i]; stack.push(treeNodes[i++]); } } } return treeNodes[0]; } }
另一种代码
public class Solution { /** 1.序列化是指通过前序遍历把二叉树变成数组 2.反序列化是指重建二叉树 */ //前序遍历序列化,null序列化为‘#’,index 为全局变量 int index; String Serialize(TreeNode root) { StringBuffer sb = new StringBuffer(); if(root == null){ sb.append("#,"); return sb.toString(); } sb.append(root.val+","); sb.append(Serialize(root.left)); sb.append(Serialize(root.right)); return sb.toString(); } TreeNode Deserialize(String str) { //先判空 if(str == null){ return null; } index = -1; String[] strSeg = str.split(","); return DeserializeStr(strSeg); } public TreeNode DeserializeStr(String[] strSeg){ index++; TreeNode treeNode = null; //字符串比较大小:== 不仅内容相同,引用地址也要相同(String类是不变类)! .equals(""):内容相同即可 if(!strSeg[index].equals("#")){ //新建此结点,字符串和包装类的转换 treeNode = new TreeNode(Integer.valueOf(strSeg[index])); treeNode.left = DeserializeStr(strSeg); treeNode.right = DeserializeStr(strSeg); } return treeNode; } }
参考资料
https://www.nowcoder.com/questionTerminal/cf7e25aa97c04cc1a68c8f040e71fb84?f=discussion