当前位置:网站首页>如何根据“前序遍历,中序遍历”,“中序遍历,后序遍历”构建按二叉树
如何根据“前序遍历,中序遍历”,“中序遍历,后序遍历”构建按二叉树
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);
}
}
边栏推荐
- Android 面试——如何写一个又好又快的日志库?
- js的new Function()常用方法
- [2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
- visual studio 2015 warning MSB3246
- win10 uwp use WinDbg to debug
- 顺序队列
- PowerCLi 导入License到vCenter 7
- 三种方式设置特定设备UWP XAML view
- 【手把手教你使用STM32HAL库的串口空闲中断】
- After the tester with 10 years of service "naked resignation" from the big factory...
猜你喜欢

LayaBox---TypeScript---Problems encountered at first contact

27.降维

数电快速入门(五)(编码器的介绍以及通用编码器74LS148和74LS147的介绍)

零基础都能拿捏的七夕浪漫代码,快去表白或去制造惊喜吧

拒绝服务攻击DDoS介绍与防范

3. Byte stream and character stream of IO stream

PowerCLi 批量配置NTP

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

Comic | Two weeks after the boss laid me off, he hired me back and doubled my salary!

嵌入式分享合集28
随机推荐
【C语言】指针和数组的深入理解(第三期)
结构体小结
Moke, dynamic image resource package display
Oreo domain name authorization verification system v1.0.6 public open source version website source code
【一起学Rust | 进阶篇 | Service Manager库】Rust专用跨平台服务管理库
js的new Function()常用方法
数字IC设计中基本运算的粗略的延时估计
2、字符集-编码-解码
PowerCLi batch configuration of NTP
Spss-系统聚类手算实操
【手把手教你使用STM32HAL库的串口空闲中断】
Big capital has begun to flee the crypto space?
Five Minutes Introductory Text Processing Three Musketeers grep awk sed
After the tester with 10 years of service "naked resignation" from the big factory...
Named routes, the role of name in components
How to understand the crawler's Scrapy framework in the simplest and most popular way?
[2022 Hangzhou Electric Power Multi-School 5 1012 Questions Buy Figurines] Application of STL
【2022牛客多校5 A题 Don‘t Starve】DP
模拟对抗之红队免杀开发实践
DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-Polyethylene Glycol-Aldehyde A hydrophobic 18-carbon phospholipid