中序:
public static String serialByPre(TreeNode head) {
if (head == null) {
return "#_";
}
String res = head.val + "_";
res += serialByPre(head.left);
res += serialByPre(head.right);
return res;
}
public static TreeNode serialByPreString(String str) {
String[] values = str.split("_");
Queue<String> queue = new LinkedList<>();
for (String value : values) {
queue.add(value);
}
return reconPreOrder(queue);
}
public static TreeNode reconPreOrder(Queue<String> queue) {
String value = queue.poll();
if (value.equals("#")) {
return null;
}
TreeNode head = new TreeNode(Integer.parseInt(value));
head.left = reconPreOrder(queue);
head.right = reconPreOrder(queue);
return head;
}