当前位置:网站首页>中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]
中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]
2022-06-29 14:55:00 【REN_林森】
前言
根据一个序列,可以通过递归划分区间,回溯拼接子树来完成树生成。
前中序遍历,一个能确定根节点,一个能拿着根节点划分左右区间,抽象的完成了前置任务,所以前中序能够确定一颗二叉搜索树。同理,中后序亦是如此,不过后序遍历根节点在后,而且遍历为左右根,所以根过了就是右,所以需要先构建右子树,再构建左子树。
一、中序和后序遍历构建二叉搜索树

二、自上而下+自下而上
// 根据中序 + 后序来构建二叉树。
class BuildTree2 {
/* target:用后序和中序遍历来构建二叉树。 后序作用?从右到左依次得到左子树上的所有根节点。 中序作用?靠着后序遍历的根节点划分左右子树,不断划分,从下到上生成二叉树。 */
public TreeNode buildTree(int[] inorder, int[] postorder) {
// assert 无重复元素。
Map<Integer, Integer> m = new HashMap<>();
// 准备工作,快速得到根节点位置,进行左右子树划分。
for (int i = 0; i < inorder.length; i++) m.put(inorder[i], i);
// 构建树
idx = postorder.length - 1;
return buildTree(inorder, postorder, 0, inorder.length - 1, m);
}
int idx = 0;
private TreeNode buildTree(int[] inorder, int[] postorder, int begin, int end, Map<Integer, Integer> m) {
if (begin > end) return null;
// 用根节点来划分左右子树,根节点由preorder[idx]得来。
int mid = m.get(postorder[idx--]);
// 得到递归生成好的左右孩子。
// 注意,后续遍历是左右根,所以先得到右子树,再得左子树,这是和前序遍历定位的区别。
TreeNode right = buildTree(inorder, postorder, mid + 1, end, m);
TreeNode left = buildTree(inorder, postorder, begin, mid - 1, m);
// 生成根节点。
TreeNode root = new TreeNode(inorder[mid]);
// 拼接上左右子树。
root.left = left;
root.right = right;
// 返回拼接好的root树。
return root;
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
总结
1)生成树基础,自上而下递归划分区间,自下而上回溯拼接左右子树。
2)前中序遍历生成树与中后序生成树的相似与不同。
参考文献
[1] LeetCode 中序和后续遍历构建二叉树
[2] 中序和前序遍历构建二叉树
边栏推荐
- Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
- 西北工业大学遭境外电邮攻击
- Northwestern Polytechnic University attacked by overseas e-mail
- MCS: multivariate random variable polynomial distribution
- MCS:离散随机变量——Poisson分布
- Construction and application of medical field Atlas of dingxiangyuan
- 服务器的数据库连不上了【服务已起、防火墙已关、端口已开、netlent 端口不通】
- MCS:离散随机变量——Binomial分布
- SOFARegistry 源码|数据同步模块解析
- Lumiprobe 活性染料丨羧酸:Sulfo-Cyanine7.5羧酸
猜你喜欢

西北工业大学遭境外电邮攻击

Lumiprobe 点击化学丨非荧光炔烃:己酸STP酯

墨滴排版

携程季报图解:净营收41亿 与疫情前相比已被“腰斩”

又拍云 Redis 的改进之路

文本预处理库spaCy的基本使用(快速入门)

数字图像处理复习

Huashu high tech rushes to the scientific innovation board: the actual controller xuxiaoshu and his son, who plan to raise 660million yuan, are both American nationals

MCS:离散随机变量——Pascal分布

Pytorch two-dimensional multi-channel convolution operation method
随机推荐
BioVendor遊離輕鏈(κ和λ)Elisa 試劑盒的化學性質
Lumiprobe 点击化学丨非荧光叠氮化物:叠氮化物-PEG3-OH
Evaluation index of high concurrency software (website, server interface)
Lumiprobe click chemistry - non fluorescent alkyne: hexanoic acid STP ester
MCS: discrete random variable - binomial distribution
Chapter IX app project test (4) test tools
高並發軟件(網站,服務器端接口)的評價指標
明德扬XILINX-K7-325T/410T核心板数据手册
华理生物冲刺科创板:年营收2.26亿 拟募资8亿
Sofaregistry source code | data synchronization module analysis
Informatics Olympiad all in one 1000: introductory test questions
Lumiprobe reactive dye - amino dye: cyanine 5 amine
揭秘百度智能测试在测试自动执行领域实践
Lumiprobe deoxyribonucleic acid phosphate CpG 1000 solid carrier
Yi Ming ang Ke rushed to Hong Kong shares: loss of 730million in the year Lilly and sunshine life insurance were shareholders
MCS: discrete random variable Poisson distribution
symfony框架安全组件(security)防火墙配置
Unity C# 基础复习26——初识委托(P447)
信息学奥赛一本通1002:输出第二个整数
知识点:PCB线路板布线都有哪些诀窍?