当前位置:网站首页>leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
2022-07-05 19:52:00 【涛涛英语学不进去】
106.从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:

难度还好,递归法。终止条件是 两个数组的长度都为 0 时,返回 null 。后序遍历 左 右 根 。每次构造后序遍历时,后序数组的最后一个总是根结点,再根据这个根结点,还有题目规定 无重复元素,则在中序数组中找到当前根结点,以它为界限把中序数组分成左子树的中序数组和右子树的中序数组。而中序数组中 根结点 的位置,刚好放到后序数组中,该位置往前 是 左子树的后序遍历,该位置包括该位置往后 直到 倒数第二个元素 是右子树的后序遍历,最后一个为根结点。
再以此左子树的先、后数组 和 右子树的先、后数组做递归即可。
package com.programmercarl.tree;
/** * @ClassName BuildTree * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 12:37 * @Version 1.0 * https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ * 106. 从中序与后序遍历序列构造二叉树 **/
public class BuildTree {
/** * @param inorder 中序遍历 1 2 * @param postorder 后序遍历 2 1 * @return */
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 0 && postorder.length == 0) {
return null;
}
//上个数组的长度是相等的,因为元素个数相同
//后序遍历的第一个元素为根结点
TreeNode root = new TreeNode(postorder[postorder.length - 1]);
//在中序遍历中找到根结点
int rootIndex = 0;
for (int i = 0; i < inorder.length; i++) {
if (root.val == inorder[i]) {
rootIndex = i;
}
}
//中序数组分成左子树和右子树
int[] leftIn = new int[rootIndex];
int[] rightIn = new int[inorder.length - rootIndex - 1];
for (int i = 0; i < inorder.length; i++) {
if (i < rootIndex) {
leftIn[i] = inorder[i];
} else if (i > rootIndex) {
rightIn[i-rootIndex-1] = inorder[i];
}
}
//后序遍历数组 前rootIndex位是左子树的,后rootIndex位是右子树
int[] leftPost = new int[rootIndex];
int[] rightPost = new int[inorder.length - rootIndex - 1];
//最后一位结点,已经作为根结点被构建了。
for (int i = 0; i < postorder.length - 1; i++) {
if (i < rootIndex) {
leftPost[i] = postorder[i];
} else {
rightPost[i - rootIndex] = postorder[i];
}
}
//构建左右子树
root.left = buildTree(leftIn, leftPost);
root.right = buildTree(rightIn, rightPost);
return root;
}
public static void main(String[] args) {
new BuildTree().buildTree(new int[]{
1, 2}, new int[]{
2, 1});
}
}

边栏推荐
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
- Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
- UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
- 面试官:Redis中集合数据类型的内部实现方式是什么?
- 挖财钱堂教育靠谱安全吗?
- [hard core dry goods] which company is better in data analysis? Choose pandas or SQL
- Worthy of being a boss, byte Daniel spent eight months on another masterpiece
- okcc呼叫中心有什么作用
- 四万字长文说operator new & operator delete
- Postman核心功能解析-参数化和测试报告
猜你喜欢

Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception

Summer Challenge database Xueba notes, quick review of exams / interviews~

redis集群模拟消息队列

Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo

Necessary skills for interview in large factories, 2022android will not die, I will not fall

Build your own website (16)
PHP uses ueditor to upload pictures and add watermarks

How to apply smart contracts more wisely in 2022?

Common - Hero Minesweeper

【硬核干货】数据分析哪家强?选Pandas还是选SQL
随机推荐
Fundamentals of shell programming (Chapter 9: loop)
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
That's awesome. It's enough to read this article
安卓面试宝典,2022Android面试笔试总结
国海证券在网上开户安全吗?
Force buckle 729 My schedule I
JMeter 常用的几种断言方法,你会了吗?
太牛了,看这篇足矣了
爬虫练习题(二)
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
挖财钱堂教育靠谱安全吗?
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
PHP uses ueditor to upload pictures and add watermarks
深度学习 卷积神经网络(CNN)基础
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
常用运算符与运算符优先级
Relationship between floating elements and parent and brother boxes
How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI
Django uses mysqlclient service to connect and write to the database