【LeetCode106】Construct Binary Tree from Inorder and Postorder Traversal★★
1.题目

2.思路
思路和LeetCode105类似,见上篇。
3.java代码
//测试
public class BuildTreeUsingInorderAndPostorder {
public static void main(String[] args) {
int[] postSort={7,4,2,5,8,6,3,1};
int[] inSort={4,7,2,1,5,3,8,6};
System.out.println(new Solution2().buildTree(postSort, inSort));
}
}
//利用中序和后序重建二叉树
class Solution2 {
public TreeNode buildTree(int[] inorder, int[] postorder) {
//参数校验
if(postorder==null||inorder==null||postorder.length!=inorder.length||postorder.length==0)
return null;
return buildTreeCore(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
}
/**
* 构建二叉树,数据输入的正确性由输入数据自己保证
*
* @param postorder 后序遍历的结果
* @param startPostorder 后序遍历的开始位置
* @param endPostorder 后序遍历的结束位置
* @param inorder 中序遍历的结果
* @param startInorder 中序遍历的开始位置
* @param endInorder 中序遍历的结束位置
* @return 二叉树的根结点
*/
private TreeNode buildTreeCore(int[] inorder,int startInorder, int endInorder,
int[] postorder, int startPostorder, int endPostorder) {
// 只有一个元素时直接返回该节点,这也是递归结束的出口标志
if(startPostorder==endPostorder){
return new TreeNode(postorder[endPostorder]);
}else{
// 记录根结点的在中序遍历中的位置
int rootIn=startInorder;
for(int i=startInorder;i<=endInorder;i++){
if(inorder[i]==postorder[endPostorder]){
rootIn=i;
break;
}
}
// 创建根结点
TreeNode root=new TreeNode(inorder[rootIn]);
// 左子树的结点个数
int leftLength=rootIn-startInorder;
if(leftLength>0){
// startPostorder, startPostorder+leftLength-1:左子树在后序序列中的起始和结束位置
root.left=buildTreeCore(inorder, startInorder, rootIn-1, postorder, startPostorder, startPostorder+leftLength-1);
}
// 右子树的结点个数
int rightLength=endInorder-rootIn;
if(rightLength>0){
// startPostorder+leftLength, endPostorder:左子树在后序序列中的起始和结束位置
root.right=buildTreeCore(inorder, rootIn+1, endInorder, postorder, startPostorder+leftLength, endPostorder-1);
}
return root;
}
}
}
//二叉树节点定义
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
【LeetCode106】Construct Binary Tree from Inorder and Postorder Traversal★★的更多相关文章
- 【树】Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根, ...
- 【Leetcode】【Medium】Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- IntelliJ idea连接操作DB2数据库
一.场景 一直都是用navicat作为数据库的连接工具,但是navicat不支持连接DB2,网上也推荐很多其它软件工具,但是因为DB2用的场景不多,这次只是开发etl工具需要测试数据库,正好发现ide ...
- vue 环境的搭建及初始化项目
其实超级简单,虽然网上很多,但是我顺便记录下相当于做笔记吧 1nodejs 的安装, 在node官网下载,点击安装,安装的时候最好选择路径在d盘 2设置环境变量 我的电脑-->属性-->系 ...
- VS发布web应用程序报:无法识别的特性“xmlns:xdt”。请注意特性名称区分大小写 或 未能将文件obj\...复制到obj\...未能找到路径
问题1:无法识别的特性“xmlns:xdt”.请注意特性名称区分大小写 问题2:未能将文件obj\...复制到obj\...未能找到路径 解决办法:将web项目文件下的obj文件夹从项目中排除,然后再 ...
- win10下解压版mysql-8.0.12安装教程
内容转载于:https://blog.csdn.net/hust_hqq/article/details/80572133 在他之上添加了一个:服务名无效的解决方法 1.官网下载安装包 网址:http ...
- Centos 如何关闭自动更新
法一 安装Centos 6.5后,系统yum自动更新状态默认为开启,若禁止系统自动更新需要手动关闭. 1.进入yum目录 [root@localhost ~]$ cd /etc/yum 2.编辑yum ...
- Windows 系统光盘刻录教程-光盘怎样刻录?刻录数据光盘用"轨道一次写入"还是"光盘一次写入"?
刻录光盘需要 DVD-RW 的光驱,并且光盘需要 DVD-R 的光盘用于刻录.刻录工具可以使用https://cn.ultraiso.net/ 来进行刻录.选择软件目录 中 工具 ,选择 刻录光盘映像 ...
- apache 访问权限出错,apache selinux 权限问题, (13) Permission Denied
今天在使用 httpd 做文件服务器的时候,发现 png 图像没有打开,但是原本www/html 文件夹内部的文件就可以打开.后来猜测是selinux 的问题,之前一直想写一篇关于selinux 的博 ...
- 用Python做股市数据分析(一)
本文由 伯乐在线 - 小米云豆粥 翻译.未经许可,禁止转载!英文出处:Curtis Miller.欢迎加入翻译组. 这篇博文是用Python分析股市数据系列两部中的第一部,内容基于我犹他大学 数学39 ...
- JSON数据提取
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写.同时也方便了机器进行解析和生成.JSON在数据交换中起到了一个载体的作用 ...
- 【PAT】B1071 小赌怡情(15 分)
水题一道,直接贴代码 #include<cstdio> #include<string.h> int main(){ //玩家的筹码数.以及 int T,K;scanf(&qu ...