// Encodes a tree to a single string. public String serialize(TreeNode root) { if(root == null) return"null"; return root.val + "," + serialize(root.left) + "," + serialize(root.right); }
// Decodes your encoded data to tree. public TreeNode deserialize(String data) { String[] req = data.split(","); //Arrays.asList 数组 -> 集合 ArrayList<String> res = newArrayList<>(Arrays.asList(req)); return dfsdeserialize(res); }
// Your Codec object will be instantiated and called as such: // Codec ser = new Codec(); // Codec deser = new Codec(); // TreeNode ans = deser.deserialize(ser.serialize(root));