描述
設(shè)計(jì)一個(gè)算法,并編寫(xiě)代碼來(lái)序列化和反序列化二叉樹(shù)。將樹(shù)寫(xiě)入一個(gè)文件被稱為“序列化”,讀取文件后重建同樣的二叉樹(shù)被稱為“反序列化”。如何反序列化或序列化二叉樹(shù)是沒(méi)有限制的,你只需要確保可以將二叉樹(shù)序列化為一個(gè)字符串, 并且可以將字符串反序列化為原來(lái)的樹(shù)結(jié)構(gòu)。
注意事項(xiàng)
There is no limit of how you deserialize or serialize a binary tree,
LintCode will take your output of serialize as the input of deserialize, it won't check the result of serialize.
樣例
給出一個(gè)測(cè)試數(shù)據(jù)樣例, 二叉樹(shù){3,9,20,#,#,15,7},表示如下的樹(shù)結(jié)構(gòu):
3
/ \
9 20
/ \
15 7
我們的數(shù)據(jù)是進(jìn)行BFS遍歷得到的。當(dāng)你測(cè)試結(jié)果wrong answer時(shí),你可以作為輸入調(diào)試你的代碼。你可以采用其他的方法進(jìn)行序列化和反序列化。
關(guān)鍵點(diǎn)
- 要知道序列化是什么
http://www.lxweimin.com/writer#/notebooks/15589466/notes/16028033/preview- 無(wú)論是序列化還是反序列化都需要用queue來(lái)控制結(jié)點(diǎn)的執(zhí)行順序,當(dāng)然queue的實(shí)現(xiàn)要根據(jù)題目需求選擇是用LinkedList()方法還是ArrayList方法,本題算法中用到大量讀取操作,若用LinkedList來(lái)實(shí)現(xiàn)queue不方便,本題用了ArrayList來(lái)實(shí)現(xiàn)queue
代碼
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
class Solution {
// 加public表示全局類,該類可以import到任何類內(nèi)
// 不加public默認(rèn)為保留類,只能被同一個(gè)包內(nèi)的其他類引用
/**
* This method will be invoked first, you should design your own algorithm
* to serialize a binary tree which denote by a root node to a string which
* can be easily deserialized by your own "deserialize" method later.
*/
public String serialize(TreeNode root) {
if (root == null) {
return "{}";
}
/* 利用for循環(huán)將樹(shù)中結(jié)點(diǎn)全部添加到隊(duì)列里面,利用隊(duì)列來(lái)控制序列化順序
* queue不是一個(gè)隊(duì)列,是一個(gè)數(shù)組只是名字是queue
*/
ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
queue.add(root);
for (int i = 0; i < queue.size(); i++) {
// queue是動(dòng)態(tài)數(shù)組用get()方法來(lái)得到下標(biāo)對(duì)應(yīng)的數(shù)值
TreeNode node = queue.get(i);
// 空結(jié)點(diǎn)跳過(guò)不執(zhí)行葉子結(jié)點(diǎn)添加的操作
if (node == null) {
continue;
}
// 無(wú)需理會(huì)葉子結(jié)點(diǎn)是不是空,把當(dāng)前非空結(jié)點(diǎn)的葉子結(jié)點(diǎn)添加到了隊(duì)列中
queue.add(node.left);
queue.add(node.right);
}
// 去掉隊(duì)列結(jié)尾的空結(jié)點(diǎn)
while (queue.get(queue.size() - 1) == null) {
queue.remove(queue.size() - 1);
}
// 根據(jù)隊(duì)列往動(dòng)態(tài)數(shù)組里添加相應(yīng)的值
// StringBuilder對(duì)象是動(dòng)態(tài)對(duì)象,允許擴(kuò)充它所封裝的字符串中字符的數(shù)量
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(queue.get(0).val);
// 在for循環(huán)前已經(jīng)將初值加入sb中,所以 i 初值應(yīng)設(shè)為1
for (int i = 1; i < queue.size(); i++) {
if (queue.get(i) == null) {
sb.append(",#");
}
else {
sb.append(",");
sb.append(queue.get(i).val);
}
}
sb.append("}");
return sb.toString();
}
/**
* This method will be invoked second, the argument data is what exactly
* you serialized at method "serialize", that means the data is not given by
* system, it's given by your own serialize method. So the format of data is
* designed by yourself, and deserialize it here as you serialize it in
* "serialize" method.
*/
public TreeNode deserialize(String data) {
// 字符用equals,數(shù)值用==
if (data.equals("{}")) {
return null;
}
// 此處需注意用點(diǎn)截取data的區(qū)間
// 字符串長(zhǎng)度用length()
String[] vals = data.substring(1, data.length() - 1).split(",");
ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
/* 新建一節(jié)點(diǎn),節(jié)點(diǎn)值為vals[0]
* Integer.parseInt()將字符串轉(zhuǎn)化為整型
*/
queue.add(root);
boolean isLeftChild = true;
// index是queue的索引
int index = 0;
// i 是vals的索引
for (int i = 1; i < vals.length; i++) {
// 數(shù)組長(zhǎng)度用length
// 此處同樣要注意先判斷是不是空結(jié)點(diǎn)再討論結(jié)點(diǎn)的子結(jié)點(diǎn)
if (!vals[i].equals("#")) {
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
if (isLeftChild) {
// 注意此處index=0,i=1
queue.get(index).left = node;
}
else {
queue.get(index).right = node;
}
queue.add(node);
/* '#'所的代表的空指針不要加入queue,沒(méi)有任何意義,
* 此時(shí)for循環(huán)遍歷的是index,即node的父節(jié)點(diǎn),
* for循環(huán)接下來(lái)還要遍歷node來(lái)確定node的子節(jié)點(diǎn)
* 此處不能忘記將node加入queue
*/
}
if (!isLeftChild) {
index++;
/* isLeftChild=false時(shí)說(shuō)明此時(shí)index對(duì)應(yīng)的結(jié)點(diǎn)的右兒子已經(jīng)添加到隊(duì)列中了,
* 應(yīng)將index往后移一位
*/
}
// i 每變化一次 isLeftChild 進(jìn)行一次變化
isLeftChild = !isLeftChild;
}
return root;
}
}
錯(cuò)誤
- 單引號(hào)里面放一個(gè)字符,雙引號(hào)里放多個(gè)字符
錯(cuò)誤
錯(cuò)誤代碼
正確代碼
需要先判斷node是否為空,若node都為空研究node左右子結(jié)點(diǎn)無(wú)意義