当前位置:网站首页>如何根据“前序遍历,中序遍历”,“中序遍历,后序遍历”构建按二叉树
如何根据“前序遍历,中序遍历”,“中序遍历,后序遍历”构建按二叉树
2022-08-04 21:10:00 【陈亦康】
如何根据前序遍历和中序遍历,或者中序遍历和后序遍历创建二叉树?大致思路如下文章;

分析:

代码如下:
/**
* 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 {
public int preIndex;
public TreeNode buildTreeChild(int[] preorder, int[] inorder, int inBegan, int inEnd){
//如果ib > ie说明递归终止
if(inBegan > inEnd){
return null;
}
//创建根节点
TreeNode root = new TreeNode(preorder[preIndex]);
//在中序遍历中找到对应的根节点下标
int InorderIndex = fundInorderIndex(inorder, preorder[preIndex], inBegan, inEnd);
//前序遍历中的下标继续往后遍历
preIndex++;
//通过递归继续创建左子树和右子树
root.left = buildTreeChild(preorder, inorder, inBegan, InorderIndex - 1);
root.right = buildTreeChild(preorder, inorder, InorderIndex + 1, inEnd);
//最后返回根结点即可
return root;
}
public int fundInorderIndex(int[] inorder, int val, int inBegan, int inEnd){
for(int i = inBegan; i <= inEnd; i++){
if(inorder[i] == val){
return i;
}
}
return -1;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeChild(preorder, inorder, 0, inorder.length - 1);
}
}
分析:
和前序与中序遍历的思路一样,但是注意的是,后序遍历中是从后向前找根结点,因此,当你写出后序遍历的顺序的时候,会发现需要先创建右子树,再创建左子树。
代码如下:
/**
* 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 {
public int preIndex;
public TreeNode buildTreeChild(int[] postorder, int[] inorder, int inBegan, int inEnd){
//如果ib > ie说明递归终止
if(inBegan > inEnd){
return null;
}
//创建根节点
TreeNode root = new TreeNode(postorder[preIndex]);
//在中序遍历中找到对应的根节点下标
int InorderIndex = fundInorderIndex(inorder, postorder[preIndex], inBegan, inEnd);
//前序遍历中的下标继续往后遍历
preIndex--;
//这里要注意,先构建左树再构建右树!
root.right = buildTreeChild(postorder, inorder, InorderIndex + 1, inEnd);
root.left = buildTreeChild(postorder, inorder, inBegan, InorderIndex - 1);
//最后返回根结点即可
return root;
}
public int fundInorderIndex(int[] inorder, int val, int inBegan, int inEnd){
for(int i = inBegan; i <= inEnd; i++){
if(inorder[i] == val){
return i;
}
}
return -1;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
preIndex = postorder.length - 1;
return buildTreeChild(postorder, inorder, 0, inorder.length - 1);
}
}
边栏推荐
- 两种白名单限流方案(redis lua限流,guava方案)
- win10 uwp use WinDbg to debug
- Moke, dynamic image resource package display
- PowerCLi 导入License到vCenter 7
- matlab drawing
- LayaBox---TypeScript---首次接触遇到的问题
- Oreo domain name authorization verification system v1.0.6 public open source version website source code
- web漏洞扫描器-awvs
- Configure laravel queue method using fort app manager
- 数电快速入门(二)(复合逻辑运算和逻辑代数的基本定律的介绍)
猜你喜欢

伺服电机矢量控制原理与仿真(1)控制系统的建立

PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning Code Analysis

某男子因用本地虚拟机做压测,惨遭字节面试官当场嘲笑

Android 面试——如何写一个又好又快的日志库?

C语言知识大全(一)——C语言概述,数据类型

LINQ to SQL (Group By/Having/Count/Sum/Min/Max/Avg操作符)

【2022杭电多校5 1012题 Buy Figurines】STL的运用

【2022杭电多校5 1003 Slipper】多个超级源点+最短路

数电快速入门(二)(复合逻辑运算和逻辑代数的基本定律的介绍)

PowerCLi import license to vCenter 7
随机推荐
数电快速入门(一)(BCD码和三种基本逻辑运算的介绍)
Data warehouse (1) What is data warehouse and what are the characteristics of data warehouse
LINQ to SQL (Group By/Having/Count/Sum/Min/Max/Avg操作符)
Named routes, the role of name in components
buu web
moke、动态图片资源打包显示
JWT主动校验Token是否过期
【1403. 非递增顺序的最小子序列】
【2022牛客多校5 A题 Don‘t Starve】DP
C语言知识大全(一)——C语言概述,数据类型
C#之app.config、exe.config和vshost.exe.config作用区别
dotnet enables JIT multi-core compilation to improve startup performance
[Data Mining] Written Exam Questions for Sohu Data Mining Engineers
After the tester with 10 years of service "naked resignation" from the big factory...
web漏洞扫描器-awvs
bracket matching
STP基本配置及802.1D生成树协议的改进
动手学深度学习_NiN
链路聚合技术及VRRP
Hands-on Deep Learning_NiN