Skip to content

Commit 031b255

Browse files
committed
280/281: edge crawling Serialize and Deserialize Binary Tree
1 parent a6c7e97 commit 031b255

6 files changed

+166
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[LeetCode solutions](http://maskray.me/blog/2014-06-29-leetcode-solutions) gives some thoughts on selected problems.
44

5-
Solved 279/279 problems.
5+
Solved 280/281 problems.
66

77
## Database
88

@@ -12,6 +12,7 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[serialize-and-deserialize-binary-tree.cc](serialize-and-deserialize-binary-tree.cc)|
1516
|296|[Best Meeting Point](https://leetcode.com/problems/best-meeting-point/)|[best-meeting-point.cc](best-meeting-point.cc)|
1617
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)|[find-median-from-data-stream.cc](find-median-from-data-stream.cc)|
1718
|294|[Flip Game II](https://leetcode.com/problems/flip-game-ii/)|[flip-game-ii.cc](flip-game-ii.cc)|

best-meeting-point.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class Solution {
1212
xs.push_back(i);
1313
ys.push_back(j);
1414
}
15-
nth_element(xs.begin(), xs.begin()+xs.size()/2, xs.end());
1615
nth_element(ys.begin(), ys.begin()+ys.size()/2, ys.end());
1716
int d = 0, rx = xs[xs.size()/2], ry = ys[ys.size()/2];
1817
for (auto x: xs)

count-complete-tree-nodes.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,29 @@ class Solution {
1010
return dl == dr ? (1 << dl) - 1 : countNodes(root->left) + countNodes(root->right) + 1;
1111
}
1212
};
13+
14+
///
15+
16+
class Solution {
17+
int f(TreeNode *x) {
18+
int h = 0;
19+
for (; x; x = x->left)
20+
h++;
21+
return h;
22+
}
23+
public:
24+
int countNodes(TreeNode *root) {
25+
int h = f(root), s = 0;
26+
while (root) {
27+
h--;
28+
if (f(root->right) == h) {
29+
s += 1 << h;
30+
root = root->right;
31+
} else {
32+
s += 1 << h-1;
33+
root = root->left;
34+
}
35+
}
36+
return s;
37+
}
38+
};

find-peak-element.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ class Solution {
1212
return l;
1313
}
1414
};
15+
16+
///
17+
18+
class Solution {
19+
public:
20+
int findPeakElement(vector<int> &a) {
21+
int l = 0, h = a.size();
22+
while (l < h-1) {
23+
int m = l+h >> 1;
24+
if (a[m-1] > a[m]) h = m;
25+
else l = m;
26+
}
27+
return l;
28+
}
29+
};

first-missing-positive.cc

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
// First Missing Positive
22
#define REP(i, n) for (int i = 0; i < (n); i++)
3-
43
class Solution {
54
public:
6-
int firstMissingPositive(int A[], int n) {
5+
int firstMissingPositive(vector<int> &a) {
6+
int n = a.size();
77
REP(i, n)
8-
while (0 < A[i] && A[i] <= n && A[A[i]-1] != A[i])
9-
swap(A[A[i]-1], A[i]);
8+
while (0 < a[i] && a[i] <= n && a[a[i]-1] != a[i])
9+
swap(a[a[i]-1], a[i]);
1010
REP(i, n)
11-
if (A[i] != i+1)
11+
if (a[i] != i+1)
1212
return i+1;
1313
return n+1;
1414
}
1515
};
16+
17+
///
18+
19+
class Solution {
20+
public:
21+
int firstMissingPositive(vector<int> &a) {
22+
int l = 0, h = a.size();
23+
while (l < h)
24+
if (a[l]-1 == l)
25+
l++;
26+
else if (l <= a[l]-1 && a[l]-1 < h && a[a[l]-1] != a[l])
27+
swap(a[l], a[a[l]-1]);
28+
else
29+
a[l] = a[--h];
30+
return l+1;
31+
}
32+
};
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Serialize and Deserialize Binary Tree
2+
class Codec {
3+
void encode(TreeNode *x, string &s) {
4+
if (! x)
5+
s += ".,";
6+
else {
7+
s += to_string(x->val)+",";
8+
encode(x->left, s);
9+
encode(x->right, s);
10+
}
11+
}
12+
TreeNode *decode(istringstream &ss) {
13+
string t;
14+
getline(ss, t, ',');
15+
if (t == ".")
16+
return NULL;
17+
auto r = new TreeNode(stoi(t));
18+
r->left = decode(ss);
19+
r->right = decode(ss);
20+
return r;
21+
}
22+
public:
23+
string serialize(TreeNode* root) {
24+
string s;
25+
encode(root, s);
26+
return s;
27+
}
28+
TreeNode* deserialize(string data) {
29+
istringstream ss(data);
30+
return decode(ss);
31+
}
32+
};
33+
34+
/// O(1) extra space, Morris pre-order traversal + edge crawling (resembling Schorr-Waite graph marking algorithm)
35+
36+
class Codec {
37+
int tag(TreeNode *x) {
38+
return ptrdiff_t(x) & 3;
39+
}
40+
TreeNode *pointer(TreeNode *x) {
41+
return (TreeNode *)(ptrdiff_t(x) & -4);
42+
}
43+
public:
44+
string serialize(TreeNode* x) {
45+
string s;
46+
while (x) {
47+
auto y = x->left;
48+
if (y) {
49+
while (y->right && y->right != x)
50+
y = y->right;
51+
if (y->right == x) {
52+
y->right = NULL;
53+
s += ".,";
54+
} else {
55+
s += to_string(x->val)+",";
56+
y->right = x;
57+
x = x->left;
58+
continue;
59+
}
60+
} else
61+
s += to_string(x->val)+",.,";
62+
x = x->right;
63+
}
64+
s += ".,";
65+
return s;
66+
}
67+
TreeNode* deserialize(string data) {
68+
istringstream ss(data);
69+
string t;
70+
getline(ss, t, ',');
71+
if (t == ".")
72+
return NULL;
73+
TreeNode *x = new TreeNode(stoi(t)), *y, *p = NULL;
74+
for(;;) {
75+
int xt = tag(x);
76+
auto xp = pointer(x);
77+
if (xt < 2) {
78+
x = (TreeNode *)(ptrdiff_t(xp) | xt+1);
79+
getline(ss, t, ',');
80+
y = t == "." ? NULL : new TreeNode(stoi(t));
81+
if (y) {
82+
(xt ? xp->right : xp->left) = p;
83+
p = x;
84+
x = y;
85+
} else
86+
(xt ? xp->right : xp->left) = NULL;
87+
} else {
88+
x = pointer(x);
89+
if (! p) break;
90+
y = x;
91+
x = p;
92+
xp = pointer(x);
93+
if (tag(x) == 1)
94+
p = xp->left, xp->left = y;
95+
else
96+
p = xp->right, xp->right = y;
97+
}
98+
}
99+
return x;
100+
}
101+
};

0 commit comments

Comments
 (0)