这道题学到了东西。

/*
一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树。
后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于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. 浅谈Abp vNext的模块化设计

    abp的模块化给我留下深刻的印象,模块化不是什么新概念,大家都习以为常,但是为什么要模块化,模块化的意义或者说目的是什么?也许我们思考得并不深入.难得的是abp不仅完美的阐述了模块化概念,而且把模块化 ...

  2. Django搭建示例项目实战与避坑细节

    Django 开发项目是很快的,有多快?看完本篇文章,你就知道了. 安装 Django 前提条件:已安装 Python. Django 使用 pip 命令直接就可以安装: pip install dj ...

  3. 单调栈高封装模板hia hia hia

    这个单调栈应该可以了,舒服舒服 #include <bits/stdc++.h> using namespace std; #define limit (400000 + 5)//防止溢出 ...

  4. 第二十章、QTableView与QStandardItemModel开发实战:展示Excel文件内容

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 在前面<第十九章.Model/View开发:QTableView的功能及属性> ...

  5. Python学习随笔:使用xlwings读取和操作Execl文件

    一.背景 有2种模块可以对Execl文件,一种是xlwt 方式,需要安装三个库文件 xlrd(读Excel)xlwt(写Excel)xlutils(修改Excel),也是网上介绍文章最多的一种方法,一 ...

  6. Leetcode学习笔记(6)

    题目1 ID112 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标 ...

  7. 题解 CF1062E Company

    \(\texttt{Solution}\) 数据结构学傻的蒟蒻来写一个新思路 这题的正解是利用多个结点的 \(lca\) 是 \(dfs\) 序最大的结点和 \(dfs\) 序最小的结点的 \(lca ...

  8. 精尽Spring MVC源码分析 - 调式环境搭建

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  9. hive实例的使用

    一.hive用本地文件进行词频统计 1.准备本地txt文件 2.启动hadoop,启动hive 3.创建数据库,创建文本表 4.映射本地文件的数据到文本 5.hql语句进行词频统计交将结果保存到结果表 ...

  10. 数据结构与算法——循环链表的算法实现(Joseph 问题)

    Joseph 问题: 如果有10 个人,按编号顺序1,2,...,10 顺时针方向围成一圈.从1 号开始顺时针方向1,2,...,9 报数,凡报数9 者出列(显然,第一个出圈为编号9 者). 最后一个 ...