Skip to content

Commit c0e298d

Browse files
committed
1 problem
1 parent 00de989 commit c0e298d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
236236
#### [326. Power of Three](https://github.com/hitzzc/go-leetcode/tree/master/power_of_three)
237237
#### [327. Count of Range Sum](https://github.com/hitzzc/go-leetcode/tree/master/count_of_range_sum)
238238
#### [329. Longest Increasing Path in a Matrix](https://github.com/hitzzc/go-leetcode/tree/master/longest_increasing_path_in_a_matrix)
239+
#### [331. Verify Preorder Serialization of a Binary Tree](https://github.com/hitzzc/go-leetcode/tree/master/verify_preorder_serialization_of_a_binary_tree)
239240

240241

241242

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
classSolution {
2+
public:
3+
boolisValidSerialization(string preorder) {
4+
int idx = 0;
5+
returnhelper(preorder, idx) ? idx == preorder.size() ? true : false : false;
6+
}
7+
8+
boolhelper(string& preorder, int& idx) {
9+
if (idx == preorder.size()) returnfalse;
10+
int start = idx;
11+
while (idx < preorder.size() && preorder[idx] != ',') ++idx;
12+
if (idx == preorder.size()) {
13+
return preorder.substr(start) == "#" ? true : false;
14+
}
15+
++idx;
16+
if (preorder.substr(start, idx-start-1) == "#")
17+
returntrue;
18+
if (!helper(preorder, idx)) returnfalse;
19+
returnhelper(preorder, idx);
20+
}
21+
};

0 commit comments

Comments
 (0)
close