public boolean isValidSerialization2(String preorder) {
        if (preorder == null) return false;
        int indegree = -1; // make up for root indegree 0
        int outdegree = 0;
        String[] tokens = preorder.split(",");
        for (String token : tokens) {
            ++indegree;
            if (indegree > outdegree) return false;
            if (!token.equals("#")) outdegree += 2;
        }
        return indegree == outdegree;
    }