【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 ...
随机推荐
- React中路由的基本使用
现在我们来搞一搞React中的路由吧,别问我为什么这木喜欢用搞这个字,因为它比较深奥. 注意下面我们使用的是React-Router-DOM React中的路由基本使用还是满简单的,零碎的小东西有点多 ...
- ArcGIS JavaScript API动态图层
矢量动态图层 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Typ ...
- 《Inside C#》笔记(十五) 非托管代码 下
二编写不安全代码 a)fixed关键字 代码中体现了fixed的用法:fixed (type* ptr= expression) { …}:type是类似int*这样的非托管类型或void类型,exp ...
- Related concepts of testing
根据是否知道源代码测试可以分为黑盒和白盒. 黑盒:功能测试. 白盒:知道源代码,要写测试代码. 根据测试的粒度. 方法测试: 单元测试: 集成测试: 系统测试: 根据测试的暴力程度. 压力测试:谷歌工 ...
- Java并发编程(十一)线程池的使用
1.new Thread的弊端如下: a. 每次new Thread新建对象性能差. b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或oom. c. 缺乏更多 ...
- git本地仓库关联多个remote,怎么用本地一个分支向不同remote不同分支推送代码
我想这个问题,是大家关注的问题,这个问题,我非常关注. 背景:在公司开发项目,我们一般都要把项目推送到公司领导创建的一个远程仓库里边去,但是我们同时也有自己的小仓库,这样的话,如何方便的将我们的代码, ...
- bs4爬虫入门
# -*- coding: utf-8 -*- """ Created on Fri Nov 16 13:35:33 2018 @author: zhen "& ...
- Postgre SQL连接服务器失败
首先这是登陆postgre sql时提示的错误信息: psql: 无法联接到服务器: Connection refused (0x0000274D/10061) 服务器是否在主机 &qu ...
- oracle 数据库 导出与导入 expdb和impdb使用方法 (服务器本机)
expdb 与exp 导出数据有区异,exp 无法导出空值表,用于客户端,expdb 只用于服务器端.备份出来的数据可再远程传输到另外一台linux 实现异地备份! 一 关于expdp和impdp ...
- javascript闭包—围观大神如何解释闭包
闭包的概念已经出来很长时间了,网上资源一大把,本着拿来主意的方法来看看. 这一篇文章 学习Javascript闭包(Closure) 是大神阮一峰的博文,作者循序渐进,讲的很透彻.下面一一剖析. 1. ...