Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added task 297.
  • Loading branch information
javadev committed Nov 26, 2021
commit e099634d6e42a52062f878dfc61e564a5f897722
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package g0201_0300.s0297_serialize_and_deserialize_binary_tree;

import com_github_leetcode.TreeNode;

/*
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
private final int OFFSET = 1000;
private final String DELIM = "*";
int offset;

// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
offset = 0;
serialize(root, sb);
return sb.toString();
}

public void serialize(TreeNode root, StringBuilder sb) {
// all nodes fit into 4 bits.
// IFF we offset at 0. So encode(val) = val + min(default - 1000)
if (root == null) {
sb.append(DELIM);
return;
}
String s = Integer.toHexString(root.val + OFFSET);
StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < 3 - s.length(); i++) {
sb2.append('0');
}
sb2.append(s);
sb.append(sb2.toString());
serialize(root.left, sb);
serialize(root.right, sb);
return;
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.charAt(offset) == '*') {
offset++;
return null;
}
TreeNode root =
new TreeNode(Integer.parseInt(data.substring(offset, offset + 3), 16) - OFFSET);
offset += 3;
root.left = deserialize(data);
root.right = deserialize(data);
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));
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
297\. Serialize and Deserialize Binary Tree

Hard

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

**Clarification:** The input/output format is the same as [how LeetCode serializes a binary tree](/faq/#binary-tree). You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg)

**Input:** root = \[1,2,3,null,null,4,5\]

**Output:** \[1,2,3,null,null,4,5\]

**Example 2:**

**Input:** root = \[\]

**Output:** \[\]

**Example 3:**

**Input:** root = \[1\]

**Output:** \[1\]

**Example 4:**

**Input:** root = \[1,2\]

**Output:** \[1,2\]

**Constraints:**

* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
* `-1000 <= Node.val <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g0201_0300.s0297_serialize_and_deserialize_binary_tree;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import com_github_leetcode.TreeNode;
import org.junit.Test;

public class CodecTest {
@Test
public void codec() {
TreeNode treeNode =
new TreeNode(1, new TreeNode(2), new TreeNode(3, new TreeNode(4), new TreeNode(5)));
Codec codec = new Codec();
String actual = codec.serialize(treeNode);
assertThat(actual, equalTo("3e93ea**3eb3ec**3ed**"));
TreeNode result = codec.deserialize(actual);
assertThat(result.toString(), equalTo("1,2,3,4,5"));
}
}