0
点赞
收藏
分享

微信扫一扫

LeetCode Top 100 Liked Questions 297. Serialize and Deserialize Binary Tree (Java版; Hard)


​​welcome to my blog​​

LeetCode Top 100 Liked Questions 297. Serialize and Deserialize Binary Tree (Java版; Hard)

题目描述

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, 
or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should
work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Example:

You may serialize the following tree:

1
/ \
2 3
/ \
4 5

as "[1,2,3,null,null,4,5]"
Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please
be creative and come up with different approaches yourself.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

剑指offer上也有这个题, 我的Deserialize(String str)写法稍有不同, 具体就是LeetCode上用了String[], 剑指offer时用的是逐个char扫描,再用Integer.parseInt(); 下面是在牛客上的写法

/*
结构信息的保存, 体现在空节点的处理上: 一定要以某种形式保存空节点!
*/
public class Solution {
String Serialize(TreeNode root) {
if(root==null)
return "#";
//前序: 根左右
return root.val+"!" + Serialize(root.left) + Serialize(root.right);
}
int index = 0;
TreeNode Deserialize(String str) {
if(str==null || index==str.length())
return null;
if(str.charAt(index)=='#'){
index++;
return null;
}
int i;
String curr="";
//这个while不用加越界判断index<str.length(); 因为序列化的过程一定在数字后面有个!
while(str.charAt(index)!='!'){
curr += str.charAt(index);
index++;
}
//跳到!下一个位置
index++;
TreeNode root = new TreeNode(Integer.valueOf(curr));
root.left = Deserialize(str);
root.right = Deserialize(str);
return root;
}
}

第一次做; 注意两个核心:1)null后面必须加逗号 2)curr不能调用trim()方法

public class Codec {

// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root==null)
//核心1: null后面必须加逗号, 为了保留结构信息; 起初我误以为递归函数非base case部分会给null加上逗号, 再一看逻辑, 非base case部分只给非null节点加逗号了! 注意逻辑啊少年!
return "null,";
String curr = ""+root.val+",";
curr = curr+serialize(root.left);
curr = curr+serialize(root.right);
//return curr.trim();
//核心2: curr不能调用trim()方法, 会删除掉结构信息! 不要一股脑想着怎么美化, 从而忽略了核心, 捡了芝麻丢了瓜, 大忌!
return curr;
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] str = data.split(",");
TreeNode root = core(str);
return root;
}
public int index=0;
public TreeNode core(String[] str){
if(index==str.length)
return null;
if(str[index].equals("null")){
index++;
return null;
}
TreeNode curr = new TreeNode(Integer.parseInt(str[index]));
index++;
curr.left = core(str);
curr.right = core(str);
return curr;
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));


举报

相关推荐

0 条评论