/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/publicclassCodec{// Encodes a tree to a single string.publicStringserialize(TreeNode root){if(root ==null){return"[]";}StringBuilder res =newStringBuilder("[");Queue<TreeNode> queue =newLinkedList<>(){{add(root);}};while(!queue.isEmpty()){TreeNode node = queue.poll();if(node !=null){
res.append(node.val +",");
queue.add(node.left);
queue.add(node.right);}else{
res.append("null,");}}
res.deleteCharAt(res.length()-1);
res.append("]");return res.toString();}// Decodes your encoded data to tree.publicTreeNodedeserialize(String data){if(data.equals("[]"))returnnull;String[] vals = data.substring(1,data.length()-1).split(",");TreeNode root =newTreeNode(Integer.parseInt(vals[0]));Queue<TreeNode> queue =newLinkedList<>(){{add(root);}};int i =1;while(!queue.isEmpty()){TreeNode node = queue.poll();if(!vals[i].equals("null")){
node.left =newTreeNode(Integer.parseInt(vals[i]));
queue.add(node.left);}
i++;if(!vals[i].equals("null")){
node.right =newTreeNode(Integer.parseInt(vals[i]));
queue.add(node.right);}
i++;}return root;}}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.deserialize(codec.serialize(root));