当前位置:网站首页>Reconstruction of binary tree
Reconstruction of binary tree
2022-07-30 00:15:00 【Ryuzaki Liuhe】
题目:
输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点.
假设输入的前序遍历和中序遍历的结果中都不含重复的数字.
示例一:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
分析:
find recursion
代码:
import java.util.HashMap;
import java.util.Map;
public class BuildTree {
// Use the hash function to easily get the subscript
Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || preorder.length == 0){
return null;
}
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i],i);
}
TreeNode root = f(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
return root;
}
private TreeNode f(int[] preorder, int l1, int r1, int[] inorder, int l2, int r2) {
if (l1 > r1 || l2 > r2){
return null;
}
TreeNode root = new TreeNode(preorder[l1]);
//Get the root node subscript
int i = map.get(preorder[l1]);
root.left = f(preorder,l1+1,l1+i-l2,inorder,l2,i-1);
root.right = f(preorder,l1+i-l2+1,r1,inorder,i+1,r2);
return root;
}
}

边栏推荐
- 利用热点事件来创作软文的3大技巧?自媒体人必看
- servlet执行详解
- CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
- Music theory & guitar skills
- Adaptive feature fusion pyramid network for multi-classes agriculturalpest detection
- Decision tree principle and code implementation
- Types and check set (set), study T treasure code
- Laravel 预防 SQL 注入
- Reading notes. This is the psychology: see through the essence of the pseudo psychology (version 10)"
- 【集训DAY16】KC‘s Can 【动态规划】
猜你喜欢

更换可执行文件glibc版本的某一次挣扎

EA&UML日拱一卒-多任务编程超入门-(8)多任务安全的数据类

Worthington弹性蛋白酶&透明质酸酶简介

Based on TNEWS 'today's headline news in Chinese short text classification

消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?

servlet执行详解

The go language (functions, closures, defer, panic/recover, recursion, structure, json serialization and deserialization)

月薪15k的阿里测试岗,面试原来这么简单

【集训DAY16】KC‘s Can 【动态规划】

Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
随机推荐
重建二叉树
opencv基本图像的滤波
Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions
i.MX6U-driver development-3-new character driver
【Incubator DAY18】Interesting exchange【Simulation】【Math】
EA&UML日拱一卒-状态图::重画按钮状态图
EA & UML Sun Arch - State Diagram :: Redraw Button State Diagram
Go language serialization and deserialization and how to change the uppercase of the json key value to lowercase if the json after serialization is empty
【经验】经验总结-经验教训
自媒体人如何打造出爆文?这3种类型的文章最容易爆
what is a .pro file in qt
中文语义匹配
He cell separation technology 丨 basic primary cell separation methods and materials
二叉排序树(C语言)
[Cloud native Kubernetes] Build a Kubernetes cluster in binary (middle) - deploy node nodes
Worthington解离酶:中性蛋白酶(分散酶)详情解析
CesiumJS ^ source read [0] 2022 - article directory and source engineering structure
How to design and implement report collaboration system for instruction set data products——Development practice of industrial collaborative manufacturing project based on instruction set IoT operating
1592. 重新排列单词间的空格
【集训DAY16】KC‘s Can 【动态规划】