题目描述
请实现两个函数,分别用来序列化和反序列化二叉树
/* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { private int idx = -1; 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) { idx++; String[] strr = str.split(","); TreeNode node = null; if(!strr[idx].equals("#")) { node = new TreeNode(Integer.valueOf(strr[idx])); node.left = Deserialize(str); node.right = Deserialize(str); } return node; } }