Skip to content

Commit 487a57e

Browse files
authored
feat: add solutions to lc problem: No.0331 (#2520)
No.0331.Verify Preorder Serialization of a Binary Tree
1 parent a47b7b9 commit 487a57e

File tree

5 files changed

+63
-84
lines changed

5 files changed

+63
-84
lines changed

solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README.md

+18-29
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,18 @@ class Solution:
8484
```java
8585
class Solution {
8686
public boolean isValidSerialization(String preorder) {
87-
String[] strs = preorder.split(",");
88-
int diff = 1;
89-
for (String s : strs) {
90-
if (--diff < 0) {
91-
return false;
92-
}
93-
if (!s.equals("#")) {
94-
diff += 2;
87+
List<String> stk = new ArrayList<>();
88+
for (String s : preorder.split(",")) {
89+
stk.add(s);
90+
while (stk.size() >= 3 && stk.get(stk.size() - 1).equals("#")
91+
&& stk.get(stk.size() - 2).equals("#") && !stk.get(stk.size() - 3).equals("#")) {
92+
stk.remove(stk.size() - 1);
93+
stk.remove(stk.size() - 1);
94+
stk.remove(stk.size() - 1);
95+
stk.add("#");
9596
}
9697
}
97-
return diff == 0;
98+
return stk.size() == 1 && stk.get(0).equals("#");
9899
}
99100
}
100101
```
@@ -134,28 +135,16 @@ func isValidSerialization(preorder string) bool {
134135
}
135136
```
136137

137-
<!-- tabs:end -->
138-
139-
### 方法二
140-
141-
<!-- tabs:start -->
142-
143-
```java
144-
class Solution {
145-
public boolean isValidSerialization(String preorder) {
146-
List<String> stk = new ArrayList<>();
147-
for (String s : preorder.split(",")) {
148-
stk.add(s);
149-
while (stk.size() >= 3 && stk.get(stk.size() - 1).equals("#")
150-
&& stk.get(stk.size() - 2).equals("#") && !stk.get(stk.size() - 3).equals("#")) {
151-
stk.remove(stk.size() - 1);
152-
stk.remove(stk.size() - 1);
153-
stk.remove(stk.size() - 1);
154-
stk.add("#");
155-
}
138+
```ts
139+
function isValidSerialization(preorder: string): boolean {
140+
const stk: string[] = [];
141+
for (const s of preorder.split(',')) {
142+
stk.push(s);
143+
while (stk.length >= 3 && stk.at(-1) === '#' && stk.at(-2) === '#' && stk.at(-3) !== '#') {
144+
stk.splice(-3, 3, '#');
156145
}
157-
return stk.size() == 1 && stk.get(0).equals("#");
158146
}
147+
return stk.length === 1 && stk[0] === '#';
159148
}
160149
```
161150

solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README_EN.md

+25-30
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Stack
47+
48+
We split the string `preorder` into an array by commas, then traverse the array. If we encounter two consecutive `'#'` and the third element is not `'#'`, we replace these three elements with a single `'#'`. This process continues until the array traversal is complete.
49+
50+
Finally, we check whether the length of the array is $1$ and whether the only element in the array is `'#'`.
51+
52+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the string `preorder`.
4753

4854
<!-- tabs:start -->
4955

@@ -62,17 +68,18 @@ class Solution:
6268
```java
6369
class Solution {
6470
public boolean isValidSerialization(String preorder) {
65-
String[] strs = preorder.split(",");
66-
int diff = 1;
67-
for (String s : strs) {
68-
if (--diff < 0) {
69-
return false;
70-
}
71-
if (!s.equals("#")) {
72-
diff += 2;
71+
List<String> stk = new ArrayList<>();
72+
for (String s : preorder.split(",")) {
73+
stk.add(s);
74+
while (stk.size() >= 3 && stk.get(stk.size() - 1).equals("#")
75+
&& stk.get(stk.size() - 2).equals("#") && !stk.get(stk.size() - 3).equals("#")) {
76+
stk.remove(stk.size() - 1);
77+
stk.remove(stk.size() - 1);
78+
stk.remove(stk.size() - 1);
79+
stk.add("#");
7380
}
7481
}
75-
return diff == 0;
82+
return stk.size() == 1 && stk.get(0).equals("#");
7683
}
7784
}
7885
```
@@ -112,28 +119,16 @@ func isValidSerialization(preorder string) bool {
112119
}
113120
```
114121

115-
<!-- tabs:end -->
116-
117-
### Solution 2
118-
119-
<!-- tabs:start -->
120-
121-
```java
122-
class Solution {
123-
public boolean isValidSerialization(String preorder) {
124-
List<String> stk = new ArrayList<>();
125-
for (String s : preorder.split(",")) {
126-
stk.add(s);
127-
while (stk.size() >= 3 && stk.get(stk.size() - 1).equals("#")
128-
&& stk.get(stk.size() - 2).equals("#") && !stk.get(stk.size() - 3).equals("#")) {
129-
stk.remove(stk.size() - 1);
130-
stk.remove(stk.size() - 1);
131-
stk.remove(stk.size() - 1);
132-
stk.add("#");
133-
}
122+
```ts
123+
function isValidSerialization(preorder: string): boolean {
124+
const stk: string[] = [];
125+
for (const s of preorder.split(',')) {
126+
stk.push(s);
127+
while (stk.length >= 3 && stk.at(-1) === '#' && stk.at(-2) === '#' && stk.at(-3) !== '#') {
128+
stk.splice(-3, 3, '#');
134129
}
135-
return stk.size() == 1 && stk.get(0).equals("#");
136130
}
131+
return stk.length === 1 && stk[0] === '#';
137132
}
138133
```
139134

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class Solution {
22
public boolean isValidSerialization(String preorder) {
3-
String[] strs = preorder.split(",");
4-
int diff = 1;
5-
for (String s : strs) {
6-
if (--diff < 0) {
7-
return false;
8-
}
9-
if (!s.equals("#")) {
10-
diff += 2;
3+
List<String> stk = new ArrayList<>();
4+
for (String s : preorder.split(",")) {
5+
stk.add(s);
6+
while (stk.size() >= 3 && stk.get(stk.size() - 1).equals("#")
7+
&& stk.get(stk.size() - 2).equals("#") && !stk.get(stk.size() - 3).equals("#")) {
8+
stk.remove(stk.size() - 1);
9+
stk.remove(stk.size() - 1);
10+
stk.remove(stk.size() - 1);
11+
stk.add("#");
1112
}
1213
}
13-
return diff == 0;
14+
return stk.size() == 1 && stk.get(0).equals("#");
1415
}
1516
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function isValidSerialization(preorder: string): boolean {
2+
const stk: string[] = [];
3+
for (const s of preorder.split(',')) {
4+
stk.push(s);
5+
while (stk.length >= 3 && stk.at(-1) === '#' && stk.at(-2) === '#' && stk.at(-3) !== '#') {
6+
stk.splice(-3, 3, '#');
7+
}
8+
}
9+
return stk.length === 1 && stk[0] === '#';
10+
}

solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution2.java

-16
This file was deleted.

0 commit comments

Comments
 (0)