当前位置:网站首页>Through middle order traversal and pre order traversal, the subsequent traversal will always build a binary tree
Through middle order traversal and pre order traversal, the subsequent traversal will always build a binary tree
2022-07-19 10:32:00 【Jingshui smart】
Construct binary tree through preorder traversal and inorder traversal
Use the root node and the first left node traversed in the middle order to obtain the node length , Right node subscript .
Traverse through the preamble ( First node ) And subsequent traversal ( The last node ) Get the root node
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
/** * Construct binary tree through preorder traversal and inorder traversal , Premise tree node values are not repeated * * @param pre The result of preorder traversal * @param preRootNodeStart The preamble traversal starts with the subscript of the root node * @param preEnd The last subscript of the preorder traversal set * @param inLeftNodeStart The middle order traverses the first left node subscript * @param inPos Mapping relationship between middle order traversal value and subscript * @return */
private TreeNode buildTree(int[] pre, int preRootNodeStart, int preEnd, int inLeftNodeStart, Map<Integer, Integer> inPos) {
if (preRootNodeStart > preEnd) return null;
TreeNode root = new TreeNode(pre[preRootNodeStart]);
// Get the subscript of the root node traversal in the middle order
int rootIdx = inPos.get(pre[preRootNodeStart]);
// Get the length of the left subtree
int leftLen = rootIdx - inLeftNodeStart;
root.left = buildTree(pre, preRootNodeStart + 1, preRootNodeStart + leftLen, inLeftNodeStart, inPos);
root.right = buildTree(pre, preRootNodeStart + leftLen + 1, preEnd, rootIdx + 1, inPos);
return root;
}
public TreeNode buildTree(int[] pre, int[] in) {
Map<Integer, Integer> inPos = new HashMap<>();
for (int i = 0; i < in.length; i++) {
inPos.put(in[i], i);
}
return buildTree(pre, 0, pre.length - 1, 0, inPos);
}
private TreeNode buildTreePrePostArray(int[] post, int postStart, int postEnd, int inStart, Map<Integer, Integer> inPos) {
if (postStart > postEnd) return null;
TreeNode root = new TreeNode(post[postEnd]);
int rootIdx = inPos.get(post[postEnd]);
int leftLen = rootIdx - inStart;
root.left = buildTreePrePostArray(post, postStart, postStart + leftLen - 1, inStart, inPos);
root.right = buildTreePrePostArray(post, postStart +leftLen, postEnd - 1, rootIdx + 1, inPos);
return root;
}
// Time: O(n). Space:O(n)
public TreeNode buildTreePrePostArray(int[] in, int[] post) {
Map<Integer, Integer> inPos = new HashMap<>();
for (int i = 0; i < in.length; i++) {
inPos.put(in[i], i);
}
return buildTreePrePostArray(post, 0, post.length - 1, 0, inPos);
}
边栏推荐
- 中职网络安全——(2022网络安全nc批量连接的脚本)免费的脚本哦~~~
- Random talk on GIS data (III)
- 快速判断站点是否存活的 3 种编程实现
- [sort] merge sort
- What is pytest? Automated testing is a must
- The select function of dplyr package in R language deletes the data columns in dataframe containing the specified string content (drop columns in dataframe)
- 发送阻塞,接收阻塞
- 【CSP-J 2021】总结
- opencv 画黑色矩形,并写上序号
- HCIA 静态综合实验报告 7.10
猜你喜欢

HCIA OSPF

C语言自定义类型详解

圆桌实录:炉边对话——如何在 Web3 实现创新

Figure an introduction to the interpretable method of neural network and a code example of gnnexplainer interpreting prediction

BEV空间内的特征级融合

荔枝音质高保真AI降噪技术分享

NJCTF 2017messager

opencv 画黑色矩形,并写上序号

如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例

YARN环境中应用程序JAR包冲突问题的分析及解决
随机推荐
Attachment handling of SAP Fiori
完全背包问题代码模板
如何解决谷歌浏览器解决跨域访问的问题
【牛客刷题】/*C语言实现字符串左旋*/
Studio 3T unlimited trial
FFmpeg录制视频、停止(VB.net,踩坑,类库——10)
通过中序遍历和前序遍历,后续遍历来构建二叉树
基于微信小程序的外卖点餐系统
Ffmpeg merges multiple videos (vb.net, class library-8)
Convert excel table to word table, and keep the formula in Excel table unchanged
Excel表格转换为Word表格,并且保留Excel表格中公式不发生变化
Crud code practice of user management based on koa2 + MySQL
AutoJs学习-动态解密
Studio 3T无限试用
中科磐云—D模块web远程代码执行漏洞解析
Distinction between private key and public key -- Explanation of private key and public key
R language uses the KAP function of epidisplay package to calculate the proportion of calculation consistency of paired contingency tables and the value of kappa statistics, and uses xtabs function to
R语言使用epiDisplay包的kap函数计算配对列联表的计算一致性的比例以及Kappa统计量的值、使用xtabs函数生成二维列联表
[PostgreSQL] PostgreSQL 15 optimizes distinct
idea展示服务端口--service