/** * 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. /* * 前序遍历 序列化编译为字符串 * */
public TreeNode deserialize(String data) { // 根据, 进行拆分 cur = 0; String[] split = data.split(","); return dfs2(split); } }
// 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));
/** * 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. public String serialize(TreeNode root) { if (root == null){ return"null"; }
if (cur < n && !split[cur].equals("null")){ node.left = newTreeNode(Integer.parseInt(split[cur])); queue.offer(node.left); } cur++; if(cur < n && !split[cur].equals("null")){ node.right = newTreeNode(Integer.parseInt(split[cur])); queue.offer(node.right); } cur++; } return root; } }
// 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));