这道题学到了东西。

/*
一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树。
后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以
唯一得确定。
对于一般的二叉树,必须是前序和中序或者后序和中序才能唯一确定,其中中序的作用就是
确定中心位置,以确定左右子树的前序(后序)的范围
但是对于BST根据性质,左子树的前序序列最大的就是1st节点,所以找到序列中第一个比
1st节点大的节点就是右子树的开始,这样就不需要中序了
还有一种方法,保存二叉树的结构信息,这种做法可以应用于任何二叉树。保存结构信息就是空节点添加一个符号代替,解码的时候就知道结构信息了
*/
//记录数据
StringBuilder s = new StringBuilder();
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
preTra(root);
return new String(s);
}
//前序遍历
public void preTra(TreeNode root)
{
if (root==null) return;
s.append(root.val);
s.append(",");
preTra(root.left);
preTra(root.right);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length()==0) return null;
String[] vals = data.split(",");
return helper(vals,0,vals.length-1);
}
//解码过程,和由前序及中序得到二叉树的过程类似
public TreeNode helper(String[] data,int sta,int end)
{
if (sta > end) return null;
//根据BST的特点,获得左右子树前序遍历序列的起止点
int rightSta = rSta(data,sta,end);
TreeNode cur = new TreeNode(Integer.parseInt(data[sta]));
cur.left = helper(data,sta+1,rightSta-1);
cur.right = helper(data,rightSta,end);
return cur;
}
public int rSta(String[] data,int sta,int end)
{
/*
第一个比根节点大的值就是右子树的开始
*/
int a = Integer.parseInt(data[sta]);
while (sta<=end)
{
if (Integer.parseInt(data[sta])>a)
break;
sta++;
}
return sta;
}

对于普通二叉树,前序(后序)+中序可以唯一确定二叉树,前序(后序)包含父子关系信息,中序包含兄弟关系信息

下边是前序+中序确定二叉树

/*
* 如果知道(中序遍历和前序遍历)或者(中序遍历和后序遍历)可以唯一确定一棵树*/
/*
由前序遍历和中序遍历确定二叉树
*/
public TreeNode PaI(int[] pre,int[] in)
{
if (pre.length==0) return null;
//只剩下一个节点,就直接返回这个节点
if (pre.length==1)
return new TreeNode(pre[0]);
//根据前序遍历的开头获得中序遍历的中心
int center = pre[0];
int c = 0;
for (int i = 0; i < in.length; i++) {
if (in[i]==center)
{
c = i;
break;
}
}
//根据中序遍历和中心位置确定左右子树的前中序遍历序列,递归求子树
//左右子树的中序遍历就是中心位置的左右子序列
//左右子树的前序遍历序列是根节点后边序列分为的两部分,长度分别与其中序遍历序列长度一样
TreeNode cur = new TreeNode(center);
cur.left = PaI(Arrays.copyOfRange(pre,1, 1 + c),Arrays.copyOfRange(in,0,c));
cur.right = PaI(Arrays.copyOfRange(pre,c+1,pre.length),Arrays.copyOfRange(in,c+1,in.length));
return cur;
}

步骤记住两点:所有节点信息都是从前序序列中获得(每次都是根据前序的1st节点信息作为根节点),中序遍历只是用来确定左右子树的子前序的起止点。

然后递归获得左右子树

[leetcode]449. Serialize and Deserialize BST设计BST的编解码的更多相关文章

  1. [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  2. [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. LeetCode 449. Serialize and Deserialize BST

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...

  4. 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

  5. 【leetcode】449. Serialize and Deserialize BST

    题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...

  6. 449. Serialize and Deserialize BST

    https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...

  7. 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. LeetCode 428. Serialize and Deserialize N-ary Tree

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ 题目: Serialization is the ...

  9. [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. 修改MongDB的数据类型

    语法: db.集合.find({"列":{$type:2}}).forEach(function(x){ x.列=parseFloat(x.列);db.order.save(x) ...

  2. XSS挑战赛(1)

    以前囫囵吞枣做过一遍,现在从头再来 第一关网址为:http://127.0.0.1/xss-labs-master/level1.php?name=test 而页面上显示了用户test,name可控, ...

  3. element ui中循环出来的表格勾选问题

    需求是这样的,一个房主屋里面有多个电表,每一个表是一个账户,一次只能给一个账户缴费,在点击go按钮进行缴费,这个时候判断是否跨表勾选,跨表格勾选则弹窗提示,反之符合需求,走缴费逻辑 上代码 <! ...

  4. 认识 Cargo-Rust构建工具和包管理器

    认识 Cargo-Rust构建工具和包管理器 上两篇文章 都有说到 hello world 程序,但是我们如果使用自己创建文件的方式创建项目,一旦文件多了,那得多麻烦,整个项目将变得难以管理.下面我来 ...

  5. P5838 [USACO19DEC]Milk Visits G

    发现是一道比较裸的树上莫队,于是就开始刚,然后发现好像是最难的一道题--(本题解用于作者加深算法理解,也欢迎各位的阅读) 题意 给你一棵树,树有点权,询问一条路径上是否有点权为 \(c\) 的点. 题 ...

  6. HDU3306 Another kind of Fibonacci

    本篇题解用于作者本人对于矩阵乘法的印象加深,也欢迎大家的阅读. 题目大意 众所周知,斐波那契数列为 \(f(0)=1\) , \(f(1)=1\) ,\(f(n)=f(n-1)+f(n-2)~(n&g ...

  7. 2020中国.NET开发者峰会近50场热点技术专题揭秘

    简介 / Summary 2014年微软组织并成立.NET基金会,微软在成为主要的开源参与者的道路上又前进了一步.2014年以来已经有众多知名公司加入.NET基金会,微软,Google,AWS三大云厂 ...

  8. webpack与vue使用

    项目基本结构目录: index.html <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  9. 珍藏的C语言编程系列教程

    本文有332个文字,大小约为2KB,预计阅读时间1分钟 这是本人珍藏的C语言.C++系列教程. 相信每个Coder的第一门编程语言就是C语言吧, 现在也依然很热门,不谈了.直接上链接,感兴趣的直接存, ...

  10. 写一个为await自动加上catch的loader逐渐了解AST以及babel

    为什么要写这个loader 我们在日常开发中经常用到async await去请求接口,解决异步.可async await语法的缺点就是若await后的Promise抛出错误不能捕获,整段代码区就会卡住 ...