当前位置:网站首页>根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
2022-06-24 13:00:00 【REN_林森】
二叉树生成->递归划分+回溯拼接
前言
二叉树的生成,一般来说是从上往下分析即划分,从下往上拼接子树,最终得到整个二叉树。考察左右子树划分 + 左右子树回溯拼接知识点。
一、根据前序&中序遍历生成二叉树

二、递归划分+回溯拼接子树
package everyday.medium;
import java.util.HashMap;
import java.util.Map;
// 用前序和中序遍历来构建二叉树。
public class BuildTree {
/* target:用前序和中序遍历来构建二叉树。 前序作用?依次得到左子树上的所有根节点。 中序作用?靠着前序遍历的根节点划分左右子树,不断划分,从下到上生成二叉树。 */
public TreeNode buildTree(int[] preorder, int[] inorder) {
// assert 无重复元素。
Map<Integer, Integer> m = new HashMap<>();
// 准备工作,快速得到根节点位置,进行左右子树划分。
for (int i = 0; i < inorder.length; i++) m.put(inorder[i], i);
// 构建树
return buildTree(preorder, inorder, 0, inorder.length - 1, m);
}
int idx = 0;
private TreeNode buildTree(int[] preorder, int[] inorder, int begin, int end, Map<Integer, Integer> m) {
if (begin > end) return null;
// 用根节点来划分左右子树,根节点由preorder[idx]得来。
int mid = m.get(preorder[idx++]);
// 得到递归生成好的左右孩子。
TreeNode left = buildTree(preorder, inorder, begin, mid - 1, m);
TreeNode right = buildTree(preorder, inorder, mid + 1, end, 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)前序遍历作用依次递归确定左右子树的根节点,中序遍历 + 根节点可起到左右子树划分作用。
参考文献
边栏推荐
- 【sdx62】WCN685X IPA不生效问题分析及解决方案
- Kotlin coordination channel
- [5g NR] 5g NR system architecture
- Return to new list
- **Puzzling little problem in unity - light and sky box
- 90%的项目经理都跳过的坑,你现在还在坑里吗?
- Tupu software is the digital twin of offshore wind power, striving to be the first
- Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
- Baidu map API drawing points and tips
- How to avoid serious network security accidents?
猜你喜欢

Eight major trends in the industrial Internet of things (iiot)

Rasa 3.x 学习系列-非常荣幸成为 Rasa contributors 源码贡献者,和全世界的Rasa源码贡献者共建共享Rasa社区!

Google waymo proposed r4d: remote distance estimation using reference target

21set classic case

Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)

Usage of multimeter

Gatling performance test

Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices

龙蜥开发者说:首次触电,原来你是这样的龙蜥社区? | 第 8 期

The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
随机推荐
AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
pip uninstall all packages except builtin package
杰理之无缝循环播放【篇】
HarmonyOS. two
Zhiyuan community weekly 86: Gary Marcus talks about three linguistic factors that can be used for reference in large model research; Google puts forward the Wensheng graph model parti which is compar
返回新列表
Simulated 100 questions and answers of fluorination process examination in 2022
Dragon lizard developer said: first time you got an electric shock, so you are such a dragon lizard community| Issue 8
2022质量员-设备方向-通用基础(质量员)考试题及答案
智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
[5g NR] 5g NR system architecture
从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
《中国数据库安全能力市场洞察,2022》报告研究正式启动
项目经理搭建团队,需要看6个特征
杰理之可能出现有些芯片音乐播放速度快【篇】
**Unity中莫名其妙得小问题-灯光和天空盒
Kotlin initialization block
谷歌WayMo提出R4D: 采用参考目标做远程距离估计
4 reasons for "safe left shift"
万用表的使用方法