LeetCode Serialize and Deserialize Binary Tree

题目:

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.

For example, you may serialize the following tree

    1
   / \
  2   3
     / \
    4   5
as  "[1,2,3,null,null,4,5]" , just the same as  how LeetCode OJ 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.

题意:

就是将一棵二叉树序列化然后反序列化能够回到原来的那棵二叉树。每一种序列化方法可以自己设置,不用一定按照题目提供的方法,比如上面那棵二叉树的层次遍历序列我们可以写成[1,2,3,null,null,4,5,null,null,null,null]。这样有利于反序列化的时候,我们可以根据节点的序号来找子节点和父节点。所以这里是个技巧,也就是在层次遍历的时候,每次都要遍历完。然后在反序列的时候,我们发现,一个节点的子节点的标号并不是2 * i + 1,这是因为前面有null节点出现,所以我们每次在进行找子节点的时候,需要先找到到底有多少null出现,因为所以真正的子节点的标号是2 * (i - num) + 1,这个num就是在这个i之前出现的null值,这是这里需要注意的一点,然后就是应用于树的一些基本技巧来做题。

public class Codec 
{
    public static String serialize(TreeNode root)
	{
		StringBuffer sb = new StringBuffer();
		LinkedList<TreeNode> list = new LinkedList<TreeNode>();
		if(root == null)
			return sb.toString();
		else
		{
			list.add(root);
			int length = list.size();
			while(!list.isEmpty())
			{
				while(length-- > 0)
				{
					TreeNode node = list.peek();
					list.poll();
					if(node == null)
					    sb.append(null + ",");
					else 
					{
						sb.append(node.val + ",");
						if(node.left != null)
							list.add(node.left);
						else 
						{
							TreeNode nodes = null;
							list.add(nodes);
						}
						if(node.right != null)
							list.add(node.right);
						else 
						{
							TreeNode nodes = null;
							list.add(nodes);
						}
					}
							//sb.append(null + ",");
				}
				length = list.size();
				//System.out.println(length);
			}
			sb.deleteCharAt(sb.length() - 1);      //这是为了去掉最后的那个逗号
			int j = sb.length() - 1;
			//System.out.println(j);
			/*while(j >= 0)
			{
				if(sb.charAt(j) == 'l')
				{
					for(int k = 0; k < 4; k++)
						sb.deleteCharAt(j - k);
					sb.deleteCharAt(j - 4);
					j = j - 5;
				}
				else 
					break;
			}*/
			return sb.toString();
		}
	}
	public static TreeNode deserialize(String data)
	{
	    if(data.length() == 0)
			return null;
		else
		{
			String[] str = data.split(",");
			int length = str.length;
			TreeNode[] node = new TreeNode[length];
			int[] nums = new int[length];    //初始化这个数组的值都为0
			int i = 0;
			while(i <= length - 1)
			{
				//System.out.println(str[i]);
				if(i > 0)
					nums[i] = nums[i - 1];   //这里有累加,因为都是在前面的基础上看有多少个0
				if(!str[i].equals("null"))
				{
					node[i] = new TreeNode(Integer.parseInt(str[i]));
					//System.out.println(node[i].val);
				}
				else
				{
					node[i] = null;
					nums[i]++;
				}
				i++;
			}
			//System.out.println(node.length);
			for(int j = 0; j < str.length; j++)
			{
				if(node[j] == null)
					continue;
				node[j].left = node[2*(j - nums[j]) + 1];    //这才是最后的子节点的标号
				node[j].right = node[2*(j - nums[j]) + 2];
			}
			return node[0];
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值