当前位置:网站首页>剑指Offer07. 重建二叉树
剑指Offer07. 重建二叉树
2022-07-03 11:50:00 【伍六琪】
剑指Offer07. 重建二叉树
题目描述
输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
示例 1:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
示例 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
限制:
0 <= 节点个数 <= 5000
JAVA代码
递归方法
参考了天勤书内的解法。
**设计思想:**先序遍历序列中第一个元素x即为树的根结点数值。在中序遍历中找到x,利用x将中序遍历的数组分为了两个子序列,左边序列构成树的左子树,右边序列构成树的右子树,再对左右两个子序列用同样的方法处理,直到处理的子序列只剩下一个元素时,二叉树构造完成。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length;
TreeNode node = createTree(preorder,inorder,0,n-1,0,n-1);
return node;
}
//构建二叉树
public TreeNode createTree(int[] preorder, int[] inorder,int L1,int R1,int L2,int R2){
TreeNode node;
int i;
if(L1>R1){
return null;
}
node = new TreeNode();
node.left=node.right=null;
for(i = L2;i<=R2;++i){
if(inorder[i]==preorder[L1]){
break;
}
}
node.val = inorder[i];
node.left = createTree(preorder,inorder,L1+1,L1+i-L2,L2,i-1);
node.right = createTree(preorder,inorder,L1+i-L2+1,R1,i+1,R2);
return node;
}
}

官方–迭代方法
迭代即非递归方法。
思路可以二叉树遍历的非递归实现作为理论基础。
二叉树的非递归方法感觉很难理解,故待后续有学习需求再来这里补充。
暂时感觉递归够用。看不懂所以不纠结浪费时间了。
边栏推荐
- ES6 standard
- DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
- AOSP ~ NTP (Network Time Protocol)
- Differences between MySQL Union and union all
- Implement verification code verification
- 232. Implement queue with stack
- Shardingsphere sub database and sub table < 3 >
- Redis
- (construction notes) learning experience of MIT reading
- 1-2 project technology selection and structure
猜你喜欢

【mysql专项】读锁和写锁

2.8 overview of ViewModel knowledge

4000 word super detailed pointer

Wechat applet - basic content

Display time with message interval of more than 1 minute in wechat applet discussion area
![[download attached] password acquisition tool lazagne installation and use](/img/21/eccf87ad9946d4177b600d96e17322.png)
[download attached] password acquisition tool lazagne installation and use

雲計算未來 — 雲原生

What is more elegant for flutter to log out and confirm again?

New features of ES6

LeetCode 0556.下一个更大元素 III - 4步讲完
随机推荐
(construction notes) grasp learning experience
(构造笔记)GRASP学习心得
QT OpenGL rotate, pan, zoom
Flutter: self study system
Solve msvcp120d DLL and msvcr120d DLL missing
PHP导出word方法(一mht)
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
ES6新特性
01_ Using the concurrent tool class library, is thread safety safe
adb push apk
【附下载】密码获取工具LaZagne安装及使用
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
regular expression
QT OpenGL texture map
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
347. Top k high frequency elements
Kubectl_ Command experience set
Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
Php Export word method (One MHT)
Why can't my MySQL container start