题解 | #序列化二叉树# | JAVA | DFS
序列化二叉树
http://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84
采用DFS遍历
用先序遍历很简单 , 因为先序是 中 , 左, 右 , 所有用index下标控制就行了。
public class Solution {
String Serialize(TreeNode root) {
if (root == null) {
return "#";
}
//用先序遍历很简单 , 因为先序是 中 , 左, 右 , 所有用index下标控制就行了。
return root.val + "," + Serialize(root.left) + "," + Serialize(root.right);
}
TreeNode Deserialize(String str) {
String[] arr = str.split(",");
return buildTree(arr);
}
public int index = 0;
private TreeNode buildTree(String[] arr) {
String curr = arr[index];
index++;
if (curr.equals("#")) {
return null;
}
TreeNode root = new TreeNode(Integer.parseInt(curr));
root.left = buildTree(arr);
root.right = buildTree(arr);
return root;
}
}
查看5道真题和解析