Skip to content

Commit 4a743b5

Browse files
[LEET-449] add 449
1 parent de14161 commit 4a743b5

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

leetcode-algorithms/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
|454|[4Sum II](https://leetcode.com/problems/4sum-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/_4SumII.java) | O(n) |O(n) | Medium| HashMap
3333
|453|[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumMovestoEqualArrayElements.java)| O(n)|O(1) | Easy|
3434
|451|[Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SortCharactersByFrequency.java) | O(nlogn) |O(n) | Medium| HashMap
35+
|449|[Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SerializeandDeserializeBST.java)| O(n)|O(h) | Medium| BFS
3536
|448|[Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindAllNumbersDisappearedinanArray.java)| O(n)|O(1) | Easy| Array, HashMap
3637
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NumberofBoomerangs.java)| O(n^2)|O(n) | Easy| HashMap
3738
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindAllDuplicatesinanArray.java)| O(n)|O(1) | Medium| Array
@@ -112,7 +113,7 @@
112113
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RemoveInvalidParentheses.java)| ? | ? | Hard| BFS
113114
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BullsandCows.java)| O(n)|O(1) | Easy|
114115
|298|[Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeLongestConsecutiveSequence.java)| O(n)|O(n) | Medium | Tree
115-
|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SerializeandDeserializeBinaryTree.java)| ?|? | Hard|
116+
|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SerializeandDeserializeBinaryTree.java)| O(n) | O(h) | Hard| BFS
116117
|296|[Best Meeting Point](https://leetcode.com/problems/best-meeting-point/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BestMeetingPoint.java)| ?|? | Hard|
117118
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindMedianFromDataStream.java)| O(nlogn) | O(n) | Hard| Heap
118119
|294|[Flip Game II](https://leetcode.com/problems/flip-game-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FlipGameII.java)| ? | ?| Medium| Backtracking

leetcode-algorithms/src/main/java/com/stevesun/common/classes/TreeNode.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ public class TreeNode {
44
public int val;
55
public TreeNode left;
66
public TreeNode right;
7-
7+
8+
@Override
9+
public String toString() {
10+
return "TreeNode{" +
11+
"val=" + val +
12+
", left=" + left +
13+
", right=" + right +
14+
'}';
15+
}
16+
817
public TreeNode(int x){this.val = x;}
918

1019
public TreeNode(TreeNode left, int val, TreeNode right) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
/**
9+
* 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.
10+
11+
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
12+
13+
The encoded string should be as compact as possible.
14+
15+
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
16+
*/
17+
public class SerializeandDeserializeBST {
18+
19+
// Encodes a tree to a single string.
20+
public String serialize(TreeNode root) {
21+
Queue<TreeNode> queue = new LinkedList<>();
22+
StringBuilder stringBuilder = new StringBuilder();
23+
if (root == null) return stringBuilder.toString();
24+
queue.offer(root);
25+
while (!queue.isEmpty()) {
26+
int size = queue.size();
27+
for (int i = 0; i < size; i++) {
28+
TreeNode curr = queue.poll();
29+
if (curr == null) {
30+
stringBuilder.append("# ");
31+
} else {
32+
stringBuilder.append(curr.val + " ");
33+
queue.offer(curr.left);
34+
queue.offer(curr.right);
35+
}
36+
}
37+
}
38+
return stringBuilder.toString();
39+
}
40+
41+
// Decodes your encoded data to tree.
42+
public TreeNode deserialize(String data) {
43+
if (data == null || data.length() == 0) return null;
44+
String[] nodes = data.split(" ");
45+
TreeNode root = new TreeNode(Integer.valueOf(nodes[0]));
46+
Queue<TreeNode> queue = new LinkedList<>();
47+
queue.offer(root);
48+
for (int i = 1; i < nodes.length; i++) {
49+
TreeNode curr = queue.poll();
50+
if (!nodes[i].equals("#")) {
51+
curr.left = new TreeNode(Integer.valueOf(nodes[i]));
52+
queue.offer(curr.left);
53+
}
54+
if (!nodes[++i].equals("#")) {
55+
curr.right = new TreeNode(Integer.valueOf(nodes[i]));
56+
queue.offer(curr.right);
57+
}
58+
}
59+
return root;
60+
}
61+
62+
}

leetcode-algorithms/src/main/java/com/stevesun/solutions/SerializeandDeserializeBinaryTree.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public class SerializeandDeserializeBinaryTree {
3131
then eventually just return the root.
3232
*/
3333

34-
3534
// Encodes a tree to a single string.
3635
public String serialize(TreeNode root) {
3736
if(root == null) return "";
@@ -77,5 +76,4 @@ public TreeNode deserialize(String data) {
7776
}
7877
return root;
7978
}
80-
8179
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
import com.stevesun.solutions.SerializeandDeserializeBST;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static junit.framework.Assert.assertEquals;
10+
11+
public class SerializeandDeserializeBSTTest {
12+
private static SerializeandDeserializeBST test;
13+
private static TreeNode actualRoot;
14+
private static TreeNode expectedRoot;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
test = new SerializeandDeserializeBST();
19+
}
20+
21+
@Before
22+
public void setupForEachTest(){
23+
}
24+
25+
@Test
26+
public void test1(){
27+
expectedRoot = new TreeNode(3);
28+
expectedRoot.left = new TreeNode(1);
29+
expectedRoot.right = new TreeNode(4);
30+
expectedRoot.left.right = new TreeNode(2);
31+
actualRoot = test.deserialize(test.serialize(expectedRoot));
32+
assertEquals(expectedRoot.toString(), actualRoot.toString());
33+
}
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy