【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★
1.题目

2.思路


3.java代码
//测试
public class BuildTreeUsingInorderAndPreorder {
public static void main(String[] args) {
int[] preSort={1,2,4,7,3,5,6,8};
int[] inSort={4,7,2,1,5,3,8,6};
System.out.println(new Solution().buildTree(preSort, inSort));
}
}
//利用前序和中序重建二叉树
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
//参数校验
if(preorder==null||inorder==null||preorder.length!=inorder.length||preorder.length==0)
return null;
return buildTreeCore(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
/**
* 构建二叉树,数据输入的正确性由输入数据自己保证
*
* @param preorder 先序遍历的结果
* @param startPreorder 先序遍历的开始位置
* @param endPreorder 先序遍历的结束位置
* @param inorder 中序遍历的结果
* @param startInorder 中序遍历的开始位置
* @param endInorder 中序遍历的结束位置
* @return 二叉树的根结点
*/
private TreeNode buildTreeCore(int[] preorder, int startPreorder, int endPreorder,
int[] inorder,int startInorder, int endInorder) {
// 只有一个元素时直接返回该节点,这也是递归结束的出口标志
if(startPreorder==endPreorder){
return new TreeNode(preorder[startPreorder]);
}else{
// 记录根结点的在中序遍历中的位置
int rootIn=startInorder;
for(int i=startInorder;i<=endInorder;i++){
if(inorder[i]==preorder[startPreorder]){
rootIn=i;
break;
}
}
// 创建根结点
TreeNode root=new TreeNode(inorder[rootIn]);
// 左子树的结点个数
int leftLength=rootIn-startInorder;
if(leftLength>0){
// startPreorder+1, startPreorder+leftLength:左子树在前序序列中的起始和结束位置
root.left=buildTreeCore(preorder, startPreorder+1, startPreorder+leftLength, inorder, startInorder, rootIn-1);
}
// 右子树的结点个数
int rightLength=endInorder-rootIn;
if(rightLength>0){
// startPreorder+leftLength+1, endPreorder:左子树在前序序列中的起始和结束位置
root.right=buildTreeCore(preorder, startPreorder+leftLength+1, endPreorder, inorder, rootIn+1, endInorder);
}
return root;
}
}
}
//二叉树节点定义
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★的更多相关文章
- 【树】Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...
- 【Leetcode】【Medium】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- 【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- leetcode105:Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- 【题解二连发】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】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 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 ...
随机推荐
- python之MRO和C3算法
python2类和python3类的区别pyhon2中才分新式类与经典类,python3中统一都是新式类Python 2.x中默认都是经典类,只有显式继承了object才是新式类python 3.x中 ...
- IE和Chrome执行javascript对鼠标双击事件的不同响应
最近在用天地图API帮同学做点开发的工作,主要是基于天地图的API实现违法用地举报的在线地图标绘,要实现的效果如下: 由于是基于天地图API的TPolygonTool工具实现面积量测和多边形绘制功能, ...
- Tsung CentOS 操作系统下搭建tsung性能测试环境_Part 1
CentOS 操作系统下搭建tsung性能测试环境_Part 1 by:授客 QQ:1033553122 步骤1.下载软件安装包 CentOS-6.0-x86_64-bin-DVD1.iso jdk- ...
- BigDecimal遇到的问题,大伙也说说
一:相除精度丢失的问题 BigDecimal的api除法相对加减乘要实现的复杂多了,只介绍常用的我遇到的问题: 问题:两数相除,如果9/3=3整除没问题,但是10/3=0.33333333...... ...
- GridSearchCV交叉验证
代码实现(基于逻辑回归算法): # -*- coding: utf-8 -*- """ Created on Sat Sep 1 11:54:48 2018 @autho ...
- Flask消息闪现
目录 Flask消息闪现 简单的例子 闪现消息的类别 过滤闪现消息 Message Flashing 参考 Flask消息闪现 一个好的应用和用户界面都需要良好的反馈.如果用户得不到足够的反馈,那么应 ...
- iframe无刷新跨域上传文件并获得返回值
原文:http://geeksun.iteye.com/blog/1070607 需求:从S平台上传文件到R平台,上传成功后R平台返回给S平台一个值,S平台是在一个页面弹出的浮窗里上传文件,所以不能用 ...
- WebService Client Generation Error with JDK8
java.lang.AssertionError: org.xml.sax.SAXParseException; systemId: jar:file:/path/to/glassfish/modul ...
- 零基础掌握百度地图兴趣点获取POI爬虫(python语言爬取)(代码篇)
好,现在进入高阶代码篇. 目的: 爬取昆明市中学的兴趣点POI. 关键词:中学 已有ak:9s5GSYZsWbMaFU8Ps2V2VWvDlDlqGaaO 昆明市坐标范围: 左下角:24.390894 ...
- data函数参数
date(format,timestamp); 参数 描述 format 必需.规定输出日期字符串的格式.可使用下列字符: d - 一个月中的第几天(从 01 到 31) D - 星期几的文本表示(用 ...