题目描述
请实现两个函数,分别用来序列化和反序列化二叉树。
解决方法:递归
先序遍历序列化二叉树。
class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public class Solution {
public int index = -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) {
index++;
int len = str.length();
if (index >= len) {
return null;
}
String[] strs = str.split(",");
TreeNode node = null;
if (!strs[index].equals("#")) {
node = new TreeNode(Integer.valueOf(strs[index]));
node.left = Deserialize(str);
node.right = Deserialize(str);
}
return node;
}
}