当前位置:网站首页>根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
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)前序遍历作用依次递归确定左右子树的根节点,中序遍历 + 根节点可起到左右子树划分作用。
参考文献
边栏推荐
- Jerry has opened a variety of decoding formats, and the waiting time from card insertion to playback is long [chapter]
- Seven challenges faced by data scientists and Solutions
- 杰理之在所有模式下打开喊话增加 mic 自动 mute【篇】
- 【5G NR】5G NR系统架构
- 钛星数安加入龙蜥社区,共同打造网络安全生态
- 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
- 居家办公更要高效-自动化办公完美提升摸鱼时间 | 社区征文
- Rasa 3.x 学习系列-非常荣幸成为 Rasa contributors 源码贡献者,和全世界的Rasa源码贡献者共建共享Rasa社区!
- The research on the report "market insight into China's database security capabilities, 2022" was officially launched
- Usage of multimeter
猜你喜欢

图扑软件数字孪生海上风电 | 向海图强,奋楫争先

Vulnerability management mistakes that CIOs still make

Antd checkbox, limit the selected quantity
![NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)](/img/b0/85ac6274b239e42c9543fa296df456.png)
NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)

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

SAP Marketing Cloud 功能概述(三)

厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构

【深度学习】NCHW、NHWC和CHWN格式数据的存储形式

遠程辦公之:在家露營辦公小工具| 社區征文

远程办公之:在家露营办公小工具| 社区征文
随机推荐
How to avoid serious network security accidents?
Getting to know cloud native security for the first time: the best guarantee in the cloud Era
Rongyun communication has "hacked" into the heart of the bank
kotlin 协程通道
吉时利静电计宽测量范围
The real project managers are all closed-loop masters!
Activity生命周期
OpenHarmony 1
真正的项目经理强者,都是闭环高手!
pip uninstall all packages except builtin package
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
Kotlin initialization block
Eight major trends in the industrial Internet of things (iiot)
The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
Kotlin coordination channel
万用表测量电阻图解及使用注意事项
[R language data science] (XIV): random variables and basic statistics
4个不可不知的“安全左移”的理由
The hidden corner of codefarming: five things that developers hate most
数商云:加强供应商管理,助推航空运输企业与供应商高效协同