Share my 10-line Java solution with explanation
If we think about the iterative approach of pre-order traversing a binary tree, whose halting condition is "stack.isEmpty() && currentNode==null", which means "
If we think about the iterative approach of pre-order traversing a binary tree, whose halting condition is "stack.isEmpty() && currentNode==null", which means "you have done everything && now you have nothing to do" -- a valid pre-order sequence should comply with this.\n\n1. If we meet "#", which means current root (the element at stack top)'s left subtree is done, we should pop this root (--stackSize) and go on traversing its right subtree. If no such root (stackSize == 0), we should stop and check whether we have traversed the entire tree (p == seq.length-1), or we are given an invalid sequence (there are still nodes we have not visited, that is p < seq.length-1).\n\n2. If we meet "some integer", it means we are visiting current root and should push it into our stack (++stackSize) and move on to its left subtree. \n\n\n----------\n\n\n    public class Solution {\n        public boolean isValidSerialization(String preorder) {\n            String[] seq = preorder.split(",");\n            int stackSize = 0, p = 0;\n            for (; p<seq.length; ++p) {\n                if (seq[p].equals("#")) { \n                    if (stackSize == 0) { break; } \n                    --stackSize;\n                }\n                else { ++stackSize; }\n            }\n            return stackSize==0 && p==seq.length-1;\n        }\n    }
[]