Skip to content

Commit 7686cc6

Browse files
committedOct 18, 2019
add 297
1 parent dd8882e commit 7686cc6

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ LeetCode
224224
|0287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | c | [c++](./src/00287-Find-the-Duplicate-Number/0287.cpp) |[python](./src/0287-Find-the-Duplicate-Number/0287.py)|||Medium|
225225
|0289|[Game of Life](https://leetcode.com/problems/game-of-life/) | c | [c++](./src/0289-Game-of-Life/0289.cpp) |[python](./src/0289-Game-of-Life/0289.py)|||Medium|
226226
|0290|[Word Pattern](https://leetcode.com/problems/word-pattern/) | c | [c++](./src/0290-Word-Pattern/0290.cpp) |[python](./src/0290-Word-Pattern/0290.py)|||Easy|
227-
|0297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | c | [c++](./src/0297-Serialize-and-Deserialize-Binary-Tree/0297.cpp) |[python](./src/0297-Serialize-and-Deserialize-Binary-Tree/0297.py)|||Hard|
227+
|0297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | c | [c++](./src/0297-Serialize-and-Deserialize-Binary-Tree/0297.cpp) |[python](./src/0297-Serialize-and-Deserialize-Binary-Tree/0297.py)||[js](./src/0297-Serialize-and-Deserialize-Binary-Tree/0297.js)|Hard|
228228
|0300|[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | c | [c++](./src/0300-Longest-Increasing-Subsequence/0300.cpp) |[python](./src/0300-Longest-Increasing-Subsequence/0300.py)|||Medium|
229229
|0303|[Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) | c | [c++](./src/0303-Range-Sum-Query-Immutable/0303.cpp) |[python](./src/0303-Range-Sum-Query-Immutable/0303.py)|||Easy|
230230
|0307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | c | [c++](./src/0307-Range-Sum-Query-Mutable/0307.cpp) |[python](./src/0307-Range-Sum-Query-Mutable/0307.py)|[go](./src/0307-Range-Sum-Query-Mutable/0307.go)|[js](./src/0307-Range-Sum-Query-Mutable/0307.js)|Medium|
@@ -275,7 +275,7 @@ LeetCode
275275
|0560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | c | [c++](./src/0560-Subarray-Sum-Equals-K/0560.cpp) |[python](./src/0560-Subarray-Sum-Equals-K/0560.py)|||Medium|
276276
|0637|[Average of Levels in Binary Tree](https://leetcode.com/problems/subarray-sum-equals-k/) | c | [c++](./src/0637-Average-of-Levels-in-Binary-Tree/0637.cpp) |[python](./src/0637-Average-of-Levels-in-Binary-Tree/0637.py)|||Easy|
277277
|0650|[2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/) | c | [c++](./src/0650-2-Keys-Keyboard/0650.cpp) |[python](./src/0650-2-Keys-Keyboard/0650.py)|||Medium|
278-
|0652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/) | c | [c++](./src/0652-Find-Duplicate-Subtrees/0652.cpp) |[python](./src/0652-Find-Duplicate-Subtrees/0652.py)|||Medium|
278+
|0652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/) | c | [c++](./src/0652-Find-Duplicate-Subtrees/0652.cpp) |[python](./src/0652-Find-Duplicate-Subtrees/0652.py)|[go](./src/0652-Find-Duplicate-Subtrees/0652.go)|[js](./src/0652-Find-Duplicate-Subtrees/0652.js)|Medium|
279279
|0653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | c | [c++](./src/0653-Two-Sum-IV-Input-is-a-BST/0653.cpp) |[python](./src/0653-Two-Sum-IV-Input-is-a-BST/0653.py)|||Easy|
280280
|0654|[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | c | [c++](./src/0654-Maximum-Binary-Tree/0654.cpp) |[python](./src/0654-Maximum-Binary-Tree/0654.py)|||Medium|
281281
|0684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/) | c | [c++](./src/0684-Redundant-Connection/0684.cpp) |[python](./src/0684-Redundant-Connection/0684.py)|||Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Codec
2+
{
3+
public:
4+
5+
// Encodes a tree to a single string.
6+
string serialize(TreeNode* root)
7+
{
8+
preOrder(root);
9+
return res;
10+
}
11+
12+
// Decodes your encoded data to tree.
13+
TreeNode* deserialize(string data)
14+
{
15+
stringstream in(data);
16+
return deOrder(in);
17+
}
18+
private:
19+
string res;
20+
21+
void preOrder(TreeNode* root)
22+
{
23+
if (root == nullptr)
24+
{
25+
res += "# "; return ;
26+
}
27+
res += to_string(root->val) + ' ';
28+
preOrder(root->left);
29+
preOrder(root->right);
30+
}
31+
32+
TreeNode* deOrder(stringstream& in)
33+
{
34+
string val;
35+
in >> val;
36+
if (val == "#") return nullptr;
37+
TreeNode* root = new TreeNode(stoi(val));
38+
root->left = deOrder(in);
39+
root->right = deOrder(in);
40+
return root;
41+
}
42+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Encodes a tree to a single string.
3+
*
4+
* @param {TreeNode} root
5+
* @return {string}
6+
*/
7+
var serialize = function(root) {
8+
let res = "";
9+
let postOrder = function(root) {
10+
if (root == null) {
11+
res += "# "; return;
12+
}
13+
postOrder(root.left);
14+
postOrder(root.right);
15+
res += root.val + " ";
16+
}
17+
postOrder(root);
18+
return res;
19+
};
20+
21+
/**
22+
* Decodes your encoded data to tree.
23+
*
24+
* @param {string} data
25+
* @return {TreeNode}
26+
*/
27+
var deserialize = function(data) {
28+
let datas = data.split(" "); datas.pop();
29+
let deOrder = function() {
30+
let val = datas.pop();
31+
if (val == "#") return null;
32+
let root = new TreeNode(Number(val));
33+
root.right = deOrder();
34+
root.left = deOrder();
35+
return root;
36+
}
37+
return deOrder();
38+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Codec:
2+
def serialize(self, root):
3+
"""Encodes a tree to a single string.
4+
5+
:type root: TreeNode
6+
:rtype: str
7+
"""
8+
res = ""
9+
def postOrder(root):
10+
nonlocal res
11+
if not root:
12+
res += '# '
13+
return
14+
postOrder(root.left)
15+
postOrder(root.right)
16+
res += str(root.val) + ' '
17+
postOrder(root)
18+
return res
19+
20+
def deserialize(self, data):
21+
"""Decodes your encoded data to tree.
22+
23+
:type data: str
24+
:rtype: TreeNode
25+
"""
26+
datas = data.split()
27+
def deOrder():
28+
val = datas.pop()
29+
if val == '#':
30+
return
31+
root = TreeNode(int(val))
32+
root.right = deOrder()
33+
root.left = deOrder()
34+
return root
35+
return deOrder()

0 commit comments

Comments
 (0)
Please sign in to comment.