Simple O(n) Solution
Use iterative preorder traversal, actually no need to use stack, just a integer to track the depth of the stack.\n\n    public class Solution {\n        public 
Use iterative preorder traversal, actually no need to use stack, just a integer to track the depth of the stack.\n\n    public class Solution {\n        public boolean isValidSerialization(String preorder) {\n            if (preorder == null || preorder.length() == 0) return false;\n            String[] strs = preorder.split(",");\n            int depth = 0;\n            int i = 0; \n            while (i < strs.length - 1) {\n                if (strs[i++].equals("#")) {\n                    if (depth == 0) return false;\n                    else depth--;\n                }\n                else depth++;\n            }\n            if (depth != 0) return false;\n            return strs[strs.length - 1].equals("#");\n        }\n    }
['Java']