当前位置:网站首页>106. 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树
2022-07-01 10:08:00 【hequnwang10】
一、题目描述
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
示例 1:
输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]
示例 2:
输入:inorder = [-1], postorder = [-1]
输出:[-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 {
Map<Integer,Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
//这与前序遍历和中序遍历构造二叉树相似
//主要找四个节点 使用hashMap保存中序遍历的节点
if(inorder == null || postorder == null || inorder.length == 0 || postorder.length == 0){
return null;
}
//保存中序遍历的节点
for(int i = 0;i<inorder.length;i++){
map.put(inorder[i],i);
}
return myBuildTree(inorder,postorder,0,inorder.length-1,0,postorder.length-1);
}
public TreeNode myBuildTree(int[] inorder, int[] postorder,int ileft,int iright,int pleft,int pright){
//判断数组
if(ileft > iright || pleft > pright){
return null;
}
//找到根节点
int rootnum = postorder[pright];
//创建根节点
TreeNode root = new TreeNode(rootnum);
//分割数组
int inorderindex = map.get(rootnum);
//左子树的长度
int leftlength = inorderindex - ileft;
//后序遍历的下一个分段
root.left = myBuildTree(inorder,postorder,ileft,inorderindex-1,pleft,pleft+leftlength-1);
root.right = myBuildTree(inorder,postorder,inorderindex+1,iright,pleft+leftlength,pright-1);
return root;
}
}
边栏推荐
- Floyd repeat
- Import and export of power platform platform sharepointlist
- Network partition notes
- Can you afford to buy a house in Beijing, Shanghai, Guangzhou and Shenzhen with an annual salary of 1million?
- 程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
- 微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
- Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
- php 实现抽奖功能
- C# 一行代码计算文件的MD5值 - CodePlus系列
- TC8:UDP_USER_INTERFACE_01-08
猜你喜欢
Swag init error: cannot find type definition: response Response
CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
Introduction of uniapp wechat applet components on demand
Initial experience of Flink, a mainstream real-time stream processing computing framework
一个悄然崛起的国产软件,低调又强大!
谁还在买“三只松鼠”们
7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
C# 一行代码计算文件的MD5值 - CodePlus系列
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
持续进阶,软通动力稳步推动云智能战略
随机推荐
STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
Daily mathematics serial 55: February 24
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
SQL statement modify field type "suggestions collection"
Who's still buying three squirrels
【黑马早报】俞敏洪称从来不看新东方股价;恒驰5将于7月开启预售;奈雪虚拟股票或涉嫌非法集资;7月1日起冰墩墩停产...
About widthstep of images in opencv
请问有没有人知道clickhouse 中 limit语句执行的逻辑,图片中,上面的SQL可以执行成功
树莓派4B系统搭建(超详细版)
CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
Graduation summary of actual combat camp
编写自己的who命令
How to understand JS promise
Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
It is interesting to understand MMAP in this way!
Who has the vision to cross the cycle?
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
A quietly rising domestic software, low-key and powerful!
The "China Mobile Chain" state secret engine was officially launched on BSN