package leetcode;

/**
 * https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
 */
public class Problem297 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public static class Codec {
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            return "";
        }

        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            return null;
        }
    }

    public static void main(String[] args) {
        Codec codec = new Codec();
        TreeNode root = new TreeNode(1);
        String serializedTree = codec.serialize(root);
        System.out.println(serializedTree);
        codec.deserialize(serializedTree);
    }
}

@@ -12,16 +12,70 @@
 tcode;%0A%0A
+import java.util.LinkedList;%0Aimport java.util.Queue;%0A%0A
 /**%0A * h
@@ -320,32 +320,138 @@
 l = x;%0A        %7D
+%0A%0A        @Override%0A        public String toString() %7B%0A            return Integer.toString(val);%0A        %7D
 %0A    %7D%0A%0A    publ
@@ -585,17 +585,812 @@
     
-return %22%22
+StringBuilder result = new StringBuilder();%0A            Queue%3CTreeNode%3E queue = new LinkedList%3C%3E();%0A            queue.add(root);%0A            while (!queue.isEmpty()) %7B%0A                TreeNode node = queue.remove();%0A                if (node == null) %7B%0A                    result.append(%22null,%22);%0A                    continue;%0A                %7D else %7B%0A                    result.append(node.val + %22,%22);%0A                %7D%0A                if (node.left != null) %7B%0A                    queue.add(node.left);%0A                %7D else %7B%0A                    queue.add(null);%0A                %7D%0A                if (node.right != null) %7B%0A                    queue.add(node.right);%0A                %7D else %7B%0A                    queue.add(node.right);%0A                %7D%0A            %7D%0A            return result.toString()
 ;%0A  
@@ -1491,24 +1491,140 @@
 ing data) %7B%0A
+            String%5B%5D nodes = data.split(%22,%22);%0A            int i = 0;%0A            if (nodes%5Bi%5D.equals(%22null%22)) %7B%0A    
             
@@ -1648,93 +1648,319 @@
     
+  
+  
 %7D%0A    
-%7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Codec codec = new Codec
+        TreeNode root = new TreeNode(Integer.parseInt(nodes%5Bi%5D));%0A            LinkedList%3CTreeNode%3E parents = new LinkedList%3C%3E();%0A            parents.add(root);%0A            i++;%0A            out:%0A            while (!parents.isEmpty()) %7B%0A                LinkedList%3CTreeNode%3E newParents = new LinkedList%3C%3E
 ();%0A
@@ -1971,137 +1971,732 @@
     
+   
-TreeNode root = new TreeNode(1);%0A        String serializedTree = codec.serialize(root);%0A        System.out.println(serializedTree
+     for (TreeNode parent : parents) %7B%0A                    if (i %3E= nodes.length) %7B%0A                        break out;%0A                    %7D%0A                    // left%0A                    if (!nodes%5Bi%5D.equals(%22null%22)) %7B%0A                        int val = Integer.parseInt(nodes%5Bi%5D);%0A                        TreeNode node = new TreeNode(val);%0A                        parent.left = node;%0A                        newParents.add(node);%0A                    %7D%0A                    i++;%0A                    if (i %3E= nodes.length) %7B%0A                        break out;%0A                    %7D%0A                    // right%0A                    if (!nodes%5Bi%5D.equals(%22null%22)) %7B%0A                        int val = Integer.parseInt(nodes%5Bi%5D
 );%0A 
@@ -2706,42 +2706,301 @@
     
+ 
-codec.deserialize(serializedTree);
+               TreeNode node = new TreeNode(val);%0A                        parent.right = node;%0A                        newParents.add(node);%0A                    %7D%0A                    i++;%0A                %7D%0A                parents = newParents;%0A            %7D%0A            return root;%0A        %7D
 %0A   

Solve problem 297