看船长的思路能听懂,但是代码看不懂,然后去看助教的更听不懂还觉得麻烦,然后在题解里面找了找一个算是跟团长思路一样的解题方法记录一下
/
var isValidSerialization = function (preorder) {
const arr = preorder.split(',');
const stack = [];
// console.log(arr, lastInx, arr[lastInx], arr[lastInx - 1], arr[lastInx - 2]);
for (let i = 0; i < arr.length; i++) {
stack.push(arr[i]);
while (stack[stack.length - 1] === '#' &&
stack[stack.length - 2] === '#') {
// console.log(stack);
stack.pop();
stack.pop();
stack[stack.length - 1] = '#';
// console.log(arr);
}
}
return stack.length === 1 && stack[0] === '#'
};