1class Solution {2public boolean isValidSerialization(String preorder) {3String[] strs = preorder.split(",");4// In starting we have one vacany for root5int vacancy = 1;67for (String str : strs) {89if (--vacancy < 0) return false;1011// whenever we encounter a new node vacancy decreases by 1 and left and right two vacancy for12// that node will added in total13if (!str.equals("#")) vacancy += 2;14}1516return vacancy == 0;17}18}