当前位置:网站首页>LeetCode刷题笔记:105.从前序与中序遍历序列构造二叉树
LeetCode刷题笔记:105.从前序与中序遍历序列构造二叉树
2022-08-03 11:30:00 【LeBron Le】
1. 问题描述
给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。
2. 解题思路
① 通过前序遍历序列的第一个节点可以得到待构造二叉树的根节点。
② 再通过中序遍历序列,由根节点将该序列分为左右两个子树。
③ 递归构建子树。
preorder | |-----------------------| |--------------------|
preL preL+1 rootIndex-inL+preL rootIndex-inL+preL+1 preR
inorder |--------------------| | |--------------------|
inL rootIndex-1 rootIndex rootIndex+1 inR
3. 实现代码
DFS 中四个 int 类型参数
① preL:前序遍历的起点
② preR:前序遍历的重点
③ inL:中序遍历的起点
④ inR:中序遍历的终点
/** * 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; * } * } */
class Solution {
HashMap<Integer, Integer> map;
private TreeNode DFS(int[] preorder, int[] inorder, int preL, int preR, int inL, int inR) {
if (preL > preR) return null;
TreeNode root = new TreeNode(preorder[preL]);
// 根节点在中序遍历中的下标
int rootIndex = map.get(preorder[preL]);
int lenLeft = rootIndex - inL;
root.left = DFS(preorder, inorder, preL + 1, lenLeft + preL, inL, rootIndex - 1);
root.right = DFS(preorder, inorder, lenLeft + preL + 1, preR, rootIndex + 1, inR);
return root;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
int len = preorder.length;
map = new HashMap<>();
for (int i = 0; i < len; i++) {
map.put(inorder[i], i);
}
return DFS(preorder, inorder, 0, len - 1, 0, len - 1);
}
}
边栏推荐
- LeetCode 899 有序队列[字典序] HERODING的LeetCode之路
- 【一起学Rust 基础篇】Rust基础——变量和数据类型
- LyScript implements memory stack scanning
- The way of programmer architecture practice: how to design a sustainable evolution system architecture?
- Generate interface documentation online
- 最牛逼的集群监控系统,它始终位列第一!
- This article takes you to understand the principle of CDN technology
- LeetCode 899 Ordered queue [lexicographical order] HERODING's LeetCode road
- [Explanation of JDBC and inner classes]
- 实现2d人物在跳跃的同时左右移动
猜你喜欢

下午见!2022京东云数据库新品发布会

本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现

【倒计时5天】探索音画质量提升背后的秘密,千元大礼等你来拿

距LiveVideoStackCon 2022 上海站开幕还有3天!

记住用户名案例(js)

2022年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

Babbitt | Metaverse daily must-read: Players leave, platforms are shut down, and the digital collection market is gradually cooling down. Where is the future of the industry?...

最牛逼的集群监控系统,它始终位列第一!

redis基础知识总结——数据类型(字符串,列表,集合,哈希,集合)

LyScript implements memory stack scanning
随机推荐
直播弱网优化
数据库一席谈:打造开源的数据生态,支撑产业数字化浪潮
用于发票处理的 DocuWare,摆脱纸张和数据输入的束缚,自动处理所有收到的发票
【输出一个整数的的每一位,由高到低输出。使用递归和不使用递归】
What is the relationship between The Matrix and 6G?
LeetCode 899 有序队列[字典序] HERODING的LeetCode之路
Traceback (most recent call last): File
增加WebView对localStorage的支持
RTP协议分析
字节最爱问的智力题,你会几道?
LP流动性挖矿DAPP系统开发丨流动性挖矿功能原理及说明
Lease recovery system based on PHP7.2+MySQL5.7
opencv学习—VideoCapture 类基础知识「建议收藏」
赛灵思MPSOC裸机下的 USB调试实验
进程内存
深度学习:文本CNN-textcnn
【无标题】函数,对象,方法的区别
使用.NET简单实现一个Redis的高性能克隆版(一)
第四周学习 HybridSN,MobileNet V1,V2,V3,SENet
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解