题目链接

题目大意:根据先序遍历和中序遍历构造二叉树。

法一:DFS。根据模拟步骤,直接从先序和中序数组中找值然后加入二叉树中,即先从先序数组中确定根结点,然后再去中序数组中确定左子树和右子树的长度,然后根据左子树和右子树的长度,去划分先序数组和中序数组,确定左子树和右子树。代码如下(耗时15ms):

     public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 0 || inorder.length == 0) {
return null;
}
return dfs(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
}
//preL是当前先序数组的第一个结点下标,preR是当前先序数组的最后一个结点下标
//inL是当前后序数组的第一个结点下标,inR是当前后序数组的最后一个结点下标
private TreeNode dfs(int[] preorder, int[] inorder, int preL, int preR, int inL, int inR) {
//将当前先序数组的第一个结点加入二叉树中,这个结点其实就是当前子树的根节点
TreeNode root = new TreeNode(preorder[preL]);
//根据这个根节点,去中序数组中找到位置下标
int rootIndex = inL;
while(inorder[rootIndex] != preorder[preL]) {
rootIndex++;
}
//左子树长度,根据当前中序数组和刚才确定的根节点下标,计算左子树长度,即中序数组中根节点前面的则是左子树
int leftLen = rootIndex - inL;
//右子树长度,根据当前中序数组和刚才确定的根节点下标,计算右子树长度,即中序数组中根节点后面的则是右子树
int rightLen = inR - rootIndex;
if(leftLen != 0) {
//确定左子树
root.left = dfs(preorder, inorder, preL + 1, preL + leftLen, inL, inL + leftLen - 1);
}
else {
root.left = null;
}
if(rightLen != 0) {
//确定右子树
root.right = dfs(preorder, inorder, preR - rightLen + 1, preR, inR - rightLen + 1, inR);
}
else {
root.right = null;
}
return root;
}

105.Construct Binary Tree from Preorder and Inorder Traversal---《剑指offer》面试6的更多相关文章

  1. 【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 ...

  2. [LeetCode] 105. 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 ...

  3. leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...

  5. LeetCode OJ 105. 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 ...

  6. 【leetocde】 105. 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 ...

  7. LeetCode 105. 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 ...

  8. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  9. (二叉树 递归) leetcode 105. 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 ...

随机推荐

  1. 【BZOJ4520】K远点对(KD-Tree)

    [BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...

  2. BZOJ1014:[JSOI2008]火星人prefix——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样 ...

  3. [zhuan]动态链接库中的.symtab和.dynsym

    http://blog.csdn.net/beyond702/article/details/50979340 原文如下: shared library (.so) "Program Lib ...

  4. ioctl函数用法小记

    By francis_hao    Aug 27,2017   UNPV1对ioctl有算是比较详细的介绍,但是,这些request和后面的数据类型是从哪里来的,以及参数具体该如何使用呢?本文尝试在不 ...

  5. 面向小数据集构建图像分类模型Keras

    文章信息 本文地址:http://blog.keras.io/building-powerful-image-classification-models-using-very-little-data. ...

  6. 在Linux系统的服务器上使用Memtester进行内存压力测试

    最近要测试一台机器的整体性能情况,就在google搜索一番,发现这个一个小工具,说是可以进行内存的压力测试,Memtester主要是捕获内存错误和一直处于很高或者很低的坏位, 其测试的主要项目有随机值 ...

  7. HDU1828线段树(扫描线)

    Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. HDU1254 bfs

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  9. Microsoft office 2013安装图解

    Microsoft office 2013安装图解... ================ 简介: Microsoft Office 2013(Office 15)是微软的新一代Office办公软件, ...

  10. 关于jmf不能播放mp3的问题解决

    想写个JAVA的MP3音乐管理器,使用JMF插件,但发现运行时总报一个异常: Unable to handle format: mpeglayer3, 44100.0 Hz, 16-bit, Ster ...