Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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‎

Lines changed: 1 addition & 0 deletions
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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool isValidSerialization(string preorder) {
4+
int idx = 0;
5+
return helper(preorder, idx) ? idx == preorder.size() ? true : false : false;
6+
}
7+
8+
bool helper(string& preorder, int& idx) {
9+
if (idx == preorder.size()) return false;
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+
return true;
18+
if (!helper(preorder, idx)) return false;
19+
return helper(preorder, idx);
20+
}
21+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /