public String serialize(TreeNode root) {
        if(root==null)return "";
        List<Integer> list= new ArrayList<>();
        Queue<TreeNode> q= new LinkedList<>();
        q.add(root);
        int level=1;
        list.add(root.val);
        while(!q.isEmpty()){
            TreeNode node=q.remove();
            level--;
            if(node!=null){
                q.add(node.left);
                q.add(node.right);
            }
            if(level==0 && !q.isEmpty()){
                level+=q.size();
                for(TreeNode x: q){
                    list.add(x== null ? null : x.val);
                }
            }
        }
        return list.stream().map(String::valueOf).collect(Collectors.joining(","));
    }
We do level order traversal including null nodes
src/problems/hard/SerializeAndDeserializeBT.java