【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 ...
随机推荐
- 商业智能BI-基础理论知识总结 ZT
因为要加入一个BI项目,所以最近在研究BI相关的知识体系,由于这个方面的知识都是比较零散,开始都很多概念,不知道从何入手,网上找的资料也不多,特别是实战案例方面更少,这里还是先把理论知识理解下吧,分享 ...
- 性能测试 Apache参数配置与性能调优
Apache性能调优 by:授客 QQ:1033553122 环境: Apache 2.4 1.选择合适的MPM(Multi -Processing Modules, 多处理模块) Unix/Linu ...
- Flutter 安装
都说程序猿学习是不分平台的,做了一辈子的Xaml,也想看看现在最牛逼的移动技术. 看了看Google 的Flutter,好像很牛逼,不怎么需要Android和IOS基础(应该还是要的), 不过现在是B ...
- recovery 恢复出厂设置失败Data wipe failed
最近客户反馈,编译32位的android系统,在recovery中执行恢复出厂设置的时候失败了,失败的打印提升信息如下. Formatting /data... [ 2.191404] E:get_f ...
- 遇到npm报错read ECONNRESET怎么办
遇到npm 像弱智一样报错怎么办 read ECONNRESET This is most likely not a problem with npm itselft 'proxy' config i ...
- 常用的docker命令
在这里记一下,以免以后忘记了. ------------------------------------------------------------------------------------ ...
- Dell XPS 13 9306安装 macOS 10.12.6
一直在考虑买一个手上的本,轻薄,办公方便.个人很喜欢mac 下的那些软件,尤其是keynote.就锁定在Macbook Pro上,但是又觉得没有什么玩头,后来一直找,觉得xps也很符合要求,就开始调查 ...
- 【第三篇】SAP ABAP7.5x新语法之程序结构&SubScreen
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文地址:SAP ABAP7.5x系列之程序结构& ...
- 项目 插件 -下载- 使用,jar包-下载
插件 -下载- 使用 BootCDN前端开源项目https://www.bootcdn.cn/all/ bootstrap:HTML.CSS 和 JS 框架 插件https://v3.bootc ...
- HTTP请求行、请求头、请求体详解(转)
转自 https://blog.csdn.net/u010256388/article/details/68491509/ HTTP请求报文解剖 HTTP请求报文由3部分组成(请求行+请求头+ ...