-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.gatsby; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
/** | ||
* @classname _449SerializeAndDeserializeBST | ||
* @description: | ||
* @author: bgm | ||
* @create: 2022/9/30 | ||
**/ | ||
|
||
class Codec { | ||
private TreeNode root; | ||
|
||
public String serialize(TreeNode root) { | ||
this.root = root; | ||
return ""; | ||
} | ||
|
||
public TreeNode root(String data) { | ||
return root; | ||
} | ||
} | ||
|
||
public class _449SerializeAndDeserializeBST { | ||
public String serialize(TreeNode root) { | ||
if (root == null) { | ||
return "#"; | ||
} | ||
Queue<TreeNode> queue = new ArrayDeque<>(); | ||
queue.add(root); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
while (queue.size() > 0) { | ||
TreeNode node = queue.poll(); | ||
stringBuilder.append("#"); | ||
if (node != null) { | ||
queue.add(root.left); | ||
queue.add(root.right); | ||
stringBuilder.append(node.val); | ||
} | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
|
||
public TreeNode deserialize(String data) { | ||
if (data.equals("#")) { | ||
return null; | ||
} | ||
String s[] = data.split("#", -1); | ||
TreeNode ans = new TreeNode(Integer.parseInt(s[1])); | ||
Queue<TreeNode> q = new LinkedList<>(); | ||
q.add(ans); | ||
for (int i = 3; i < s.length; i += 2) { | ||
TreeNode t = q.poll(); | ||
if (s[i - 1].length() > 0) { | ||
t.left = new TreeNode(Integer.parseInt(s[i - 1])); | ||
q.add(t.left); | ||
} | ||
if (s[i].length() > 0) { | ||
t.right = new TreeNode(Integer.parseInt(s[i])); | ||
q.add(t.right); | ||
} | ||
} | ||
return ans; | ||
} | ||
} |