当前位置:网站首页>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;
}
}
边栏推荐
- C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列
- Is it safe to buy funds on the access letter?
- php 实现抽奖功能
- 数据中台咋就从“小甜甜”变成了“牛夫人”?
- Kotlin 协程调度切换线程是时候解开真相了
- 超标量处理器设计 姚永斌 第4章 分支预测 --4.1 小节摘录
- Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
- Graduation summary of actual combat camp
- Eat a rich woman's melon...
- 关于#数据库#的问题:GBase 8s中如何避免死锁
猜你喜欢

Hardware midrange project

The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious

云原生到底是什么?它会是未来发展的趋势吗?

我喜欢两个男人。。。

好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与!

uniapp微信小程序组件按需引入

Floyd repeat

Cortex M4 systick details

Strange, why is the ArrayList initialization capacity size 10?

Some tools used in embedded development
随机推荐
7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
直播管理项目
Who's still buying three squirrels
Finally, someone made it clear what DRAM and NAND flash are
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
Introduction to expressions and operators in C language
bash: ln: command not found
在通达信上买基金安全吗?
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
Swag init error: cannot find type definition: response Response
TC8:UDP_ USER_ INTERFACE_ 01-08
Packetdrill script analysis guide
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
Computer USB, HDMI, DP various interfaces and speeds
Precautions for lvgl v8.2 string display on keil MDK (take little bear pie as an example)
Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
SSH服务器拒绝密码,再试一次;PermitRootLogin yes无效问题
Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
Project procurement management
硬件中台项目