【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 ...
随机推荐
- 从零开始学习html(三) 认识标签(第二部分)
一.使用ul,添加新闻信息列表 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Ty ...
- 使用CSS兄弟选择器完成复杂垂直边距(vertical margins)的设计
-------------------sibling选择器如何在完成复杂设计要求的同时,保持CSS可读 这是web前端开发过程中开始简单逐步变的复杂的例子之一:将一篇文章中的所有元素应用垂直边距(ve ...
- The packaging and installation process of Android programs
D:\android\adt-bundle-windows-x86-20131019\sdk\platform-tools工具的路径. 安卓工程经过eclipse编译然后通过aapt工具打包生成一个. ...
- CPUFreq驱动
CPUFreq子系统位于 drivers/cpufreq目录下,负责进行运行过程中CPU频率和电压的动态调整,即DvFS( Dynamic Voltage Frequency Scaling,动态电压 ...
- DMA与cache一致性的问题
Cache和DMA本身似乎是两个毫不相关的事物.Cache被用作CPU针对内存的缓存利用程序的空间局部性和时间局部性原理,达到较高的命中率,从而避免CPU每次都必须要与相对慢速的内存交互数据来提高数据 ...
- [SequenceFile_3] MapFile
0. 说明 MapFile 介绍 && 测试 1. 介绍 对 MapFile 的介绍如下: MapFile 是带有索引的 SequenceFile MapFile 是排序的 Seque ...
- 05LaTeX学习系列之---TeX的命令行操作
目录 目录 前言 (一)查看版本号 1.查看TeX的版本号 2.查看LaTeX的版本号 3.查看XeLeTeX的版本号 (二)更行版本 (三)用命令行来编译.tex文件 1.用LaTeX编译 2.用X ...
- C++中的istringstream
istringstream用于执行C++风格的串流操作. 下面的示例是使用一个字符串初始化istringstream类,然后再使用>>操作符来依次输出字符串中的内容. temp_mon=& ...
- Android Studio入门问题汇总
1.如何设置 AS 中的字体大小 2.如何切换 AS 的皮肤颜色,默认为黑色,修改为白色,改为 default 3.首次安装 Android Studio并打开时,如果创建了一个新工程并将工程保存在另 ...
- cocos2d-x3.0 Vector和Map简单使用
Vector<Node*> vec; auto node1 = Node::create(); node1->setTag(1); vec.pushBack(node1); auto ...