1
class Solution {
2
public boolean isValidSerialization(String preorder) {
3
String[] strs = preorder.split(",");
4
// In starting we have one vacany for root
5
int vacancy = 1;
6

7
for (String str : strs) {
8

9
if (--vacancy < 0) return false;
10

11
// whenever we encounter a new node vacancy decreases by 1 and left and right two vacancy for
12
// that node will added in total
13
if (!str.equals("#")) vacancy += 2;
14
}
15

16
return vacancy == 0;
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0