当前位置:网站首页>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;
}
}
边栏推荐
- Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
- Huawei accounts work together at multiple ends to create a better internet life
- Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
- [fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States
- 一个悄然崛起的国产软件,低调又强大!
- 项目必用的全局异常处理器,你学会了吗
- What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
- Dotnet console uses microsoft Maui. Getting started with graphics and skia
- 【Laravel 】faker数据填充详解
- Po mode deep encapsulation
猜你喜欢

scratch大鱼吃小鱼 电子学会图形化编程scratch等级考试二级真题和答案解析2022年6月

Packetdrill script analysis guide

Hardware midrange project

渗透常用工具-Goby

硬件中台项目

年薪100万,在北上广深买的起房子吗?

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

【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Ba

Introduction of uniapp wechat applet components on demand

零基础入门测试该学什么?最全整理,照着学就对了
随机推荐
在中金证券上做基金定投安全吗?
MySQL interception_ MySQL method for intercepting strings [easy to understand]
Have you learned the necessary global exception handler for the project
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan! Samsung sk of South Korea competes for salary increase to retain semiconductor talents;
venv: venv 的目录结构
Change password of MySQL version 5.7 and 8.0
php 实现抽奖功能
CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly
SQL statement modify field type "suggestions collection"
这样理解mmap,挺有意思!
Centos 配置discuz 提示请检查 mysql 模块是否正确加载
编写自己的who命令
Thread Basics
【Laravel 】faker数据填充详解
What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in
What is cloud primordial? Will it be the trend of future development?
MySQL common commands
机器学习之线性回归详解
大佬们 有没有搞过sink分流写入clickhouse 或者其他数据库的操作。
在通达信上买基金安全吗?