当前位置:网站首页>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# 一行代码计算文件的MD5值 - CodePlus系列
- [fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States
- Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
- 在中金证券上做基金定投安全吗?
- PHP 字符串与二进制相互转换
- IPv6 learning notes
- tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
- 机器学习之线性回归详解
- Computer USB, HDMI, DP various interfaces and speeds
- Comparison between Oracle JDK and openjdk
猜你喜欢

谁还在买“三只松鼠”们

leetcode:111. 二叉树的最小深度

架构实战营 模块九:设计电商秒杀系统

程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...

IDEA运行报错Command line is too long. Shorten command line for...

A quietly rising domestic software, low-key and powerful!

我喜欢两个男人。。。

Import and export of power platform platform sharepointlist

Introduction to expressions and operators in C language

Fried money, lost 10million.
随机推荐
京东与腾讯续签三年战略合作协议;起薪涨至26万元!韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条...
[unity rendering] customized screen post-processing
BSN长话短说之十:如何保证NFT的安全
Continue to advance, and softcom power steadily promotes cloud intelligence strategy
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;
Eat a rich woman's melon...
uniapp微信小程序组件按需引入
Import and export of power platform platform sharepointlist
Which securities company has a low, safe and reliable Commission for stock trading and account opening
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
Have you learned the necessary global exception handler for the project
How do clients request databases?
[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
PHP 字符串与二进制相互转换
STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
12.Gateway新一代网关
SSH服务器拒绝密码,再试一次;PermitRootLogin yes无效问题
Po mode deep encapsulation
mysql cdc能把能把op字段拿出来吗