Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Codec {
- // Encodes a tree to a single string.
- public String serialize(TreeNode root) {
- List<String> result = new ArrayList<>();
- Queue<TreeNode> q = new LinkedList<>();
- q.add(root);
- while (!q.isEmpty()) {
- int preSize = q.size();
- for (int i = 0; i < preSize; i++) {
- TreeNode curr = q.poll();
- if (curr == null) {
- result.add("null");
- } else {
- result.add(String.valueOf(curr.val));
- q.add(curr.left);
- q.add(curr.right);
- }
- }
- }
- System.out.println(String.join(",", result));
- return String.join(",", result);
- }
- // Decodes your encoded data to tree.
- public TreeNode deserialize(String data) {
- if (data == null) return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement