ðð297. äºåæ çåºååäžååºåå
2022幎10æ10æ¥
- algorithm
ðð297. äºåæ çåºååäžååºåå
éŸåºŠ: ðð
é®é¢æè¿°
è§£æ³ 1 - å±åº
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
// æè·¯ïŒ
// å±åºéå - åºåå
// 空èç¹ä¹å
¥é
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
if(root == null) {
return sb.toString();
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
int len = queue.size();
for(int i = 0; i < len; i ++) {
TreeNode cur = queue.poll();
if(cur == null) {
sb.append('#').append(",");
} else {
sb.append(cur.val).append(",");
queue.offer(cur.left);
queue.offer(cur.right);
}
}
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data.length() == 0) {
return null;
}
String[] array = data.split(",");
TreeNode root = new TreeNode(Integer.valueOf(array[0]));
// ä»ç¶åå© èŸ
å©éå
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int index = 1; // é€å»æ ¹èç¹ç äžæ 0ïŒä» 1 åŒå§
while(!queue.isEmpty()) {
TreeNode cur = queue.poll();
String left = array[index];
index ++;
String right = array[index];
index ++;
if(!left.equals("#")) {
cur.left = new TreeNode(Integer.valueOf(left));
queue.offer(cur.left);
}
if(!right.equals("#")) {
cur.right = new TreeNode(Integer.valueOf(right));
queue.offer(cur.right);
}
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));
èŸåº 1
è§£æ³ 2 - éåœ
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
// æè·¯ïŒ
// å
åºéå - 空èç¹ä¹å
¥ç»æé
int index = 0;
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null) {
return "";
}
StringBuilder sb = new StringBuilder();
mySol1(root, sb);
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
private void mySol1(TreeNode root, StringBuilder sb) {
// éåœç»æ¢æ¡ä»¶
if(root == null) {
sb.append("#").append(",");
return;
}
sb.append(root.val).append(",");
mySol1(root.left, sb);
mySol1(root.right, sb);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data.length() == 0) {
return null;
}
String[] array = data.split(",");
index = 0;
return mySol2(array);
}
private TreeNode mySol2(String[] array) {
String cur = array[index];
if(cur.equals("#")) {
index ++;
return null;
}
int val = Integer.valueOf(cur);
TreeNode res = new TreeNode(val);
index ++;
res.left = mySol2(array);
res.right = mySol2(array);
return res;
}
}
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));