当前位置:网站首页>LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
2022-07-28 13:06:00 【碳烤小肥羊。。。】
从前序与中序遍历序列构造二叉树

代码实现:
import java.util.HashMap;
import java.util.Map;
/** * @Author Mr.Lu * @Date 2022/7/27 11:36 * @ClassName InorderAndPostOrderBuildTreeTest * @Version 1.0 */
public class InorderAndPreOrderBuildTreeTest {
Map<Integer, Integer> map; // 方便根据数值查找位置
public TreeNode buildTree(int[] preorder, int[] inorder){
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
// 用map保存中序序列的数值对应位置
map.put(inorder[i], i);
}
return findNode(inorder, 0, inorder.length, preorder, 0, preorder.length); // 前闭后开
}
private TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] preorder, int preBegin, int preEnd) {
// 参数里的范围都是前闭后开
if(preBegin >= preEnd || inBegin >= inEnd){
// 不满足左闭右开,说明没有元素,返回空树
return null;
}
int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历的位置
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造节点
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数
root.left = findNode(inorder, inBegin, rootIndex, preorder, preBegin + 1, preBegin + lenOfLeft + 1); // 递归处理中序遍历和前序遍历的左边数组
root.right = findNode(inorder, rootIndex + 1, inEnd, preorder, preBegin + lenOfLeft + 1, preEnd); // 递归处理中序遍历和前序遍历的右面数组
return root;
}
}
从中序与后序遍历序列构造二叉树

代码实现:
import java.util.HashMap;
import java.util.Map;
/** * @Author Mr.Lu * @Date 2022/7/27 11:36 * @ClassName InorderAndPostOrderBuildTreeTest * @Version 1.0 */
public class InorderAndPostOrderBuildTreeTest {
Map<Integer, Integer> map; // 方便根据数值查找位置
public TreeNode buildTree(int[] inorder, int[] postorder){
map = new HashMap<>();
for(int i = 0; i < inorder.length; i++){
map.put(inorder[i], i);
}
return findNode(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
// 参数里的范围都是前闭后开
if(inBegin >= inEnd || postBegin >= postEnd){
// 不满足左闭右开,说明没有元素,直接返回
return null;
}
int rootIndex = map.get(postorder[postEnd - 1]); // 找到后续遍历的最后一个元素在中序遍历中的位置
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造节点
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后续数列的个数
root.left = findNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin + lenOfLeft);
root.right = findNode(inorder,rootIndex + 1, inEnd, postorder, postBegin + lenOfLeft, postEnd - 1);
return root;
}
}
边栏推荐
- Vite configuring path aliases in the project
- leetcode-深度优先与广度优先遍历
- 30 day question brushing plan (II)
- SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
- The domestic API management tool eolink is very easy to use, creating an efficient research and development tool
- Qt5开发从入门到精通——第一篇概述
- 目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)
- POJ3275 Ranking the Cows题解
- UVA11175有向图D和E From D to E and Back题解
- Duplicate data in leetcode (442) array
猜你喜欢

No swagger, what do I use?

Socket类关于TCP字符流编程的理解学习

Continuous (integration -- & gt; delivery -- & gt; deployment)

After finishing, help autumn move, I wish you call it an offer harvester

Slam thesis collection

第六章 支持向量机

产品经理:岗位职责表

30天刷题计划(二)

30 day question brushing plan (III)

【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)
随机推荐
Istio IV fault injection and link tracking
最强分布式锁工具:Redisson
url相关知识点
30天刷题计划(二)
[security] read rfc6749 and understand the authorization code mode under oauth2.0
Long closed period private placement products reappearance industry insiders have different views
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
JWT login authentication + token automatic renewal scheme, well written!
Merge table rows - three levels of for loop traversal data
产品经理:岗位职责表
【Try to Hack】HFish蜜罐部署
RSA用私钥加密数据公钥解密数据(不是签名验证过程)
To build agile teams, these methods are indispensable
多线程与高并发(三)—— 源码解析 AQS 原理
Several efficient APIs commonly used in inventory operation URL
在 Kubernetes 中部署应用交付服务(第 1 部分)
Security assurance is based on software life cycle - networkpolicy application
vite在项目中配置路径别名
阿里、京东、抖音:把云推向产业心脏
How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question