7 lines Easy Java Solution
Some used stack. Some used the depth of a stack. Here I use a different perspective. In a binary tree, if we consider null as leaves, then\n\n all non-null node
Some used stack. Some used the depth of a stack. Here I use a different perspective. In a binary tree, if we consider null as leaves, then\n\n* all non-null node provides 2 outdegree and 1 indegree (2 children and 1 parent), except root\n* all null node provides 0 outdegree and 1 indegree (0 child and 1 parent).\n\nSuppose we try to build this tree. During building, we record the difference between out degree and in degree `diff` = `outdegree - indegree`. When the next node comes, we then decrease `diff` by 1, because the node provides an in degree. If the node is not `null`, we increase diff by `2`, because it provides two out degrees. If a serialization is correct, diff should never be negative and diff will be zero when finished.\n\n\n    public boolean isValidSerialization(String preorder) {\n        String[] nodes = preorder.split(",");\n        int diff = 1;\n        for (String node: nodes) {\n            if (--diff < 0) return false;\n            if (!node.equals("#")) diff += 2;\n        }\n        return diff == 0;\n    }
['Java']