题目链接

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

法一: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. 【bzoj1705】[Usaco2007 Nov]Telephone Wire 架设电话线 dp

    题目描述 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= N < ...

  2. [APIO2015]巴厘岛的雕塑 贪心+DP+特殊数据优化

    写了好久.... 刚刚调了一个小时各种对拍,,,,最后发现是多写了一个等号,,,,内心拒绝 表示一开始看真的是各种懵逼啊 在偷听到某位大佬说的从高位开始贪心后发现可做 首先考虑小数据(因为可以乱搞) ...

  3. Html CSS学习(五)position定位 原

    Html CSS学习(五)position定位 position用来对元素进行定位,其值有以下几种: static:无特殊定位,对象遵循正常文档流,top,right,bottom,left等属性不会 ...

  4. BZOJ1013:[JSOI2008]球形空间产生器——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1013 Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在 ...

  5. App.config的典型应用

    ----.net中XML的典型应用 第一种修改方式: 添加xml节点figguration内容, 微软提供了一种方案来读取connectionStrings里的内容 这样就可以拿到连接sql serv ...

  6. 51nod 1275 连续子段的差异(twopointer+单调队列)

    对于每一个i找到最近的j满足最大值-最小值>K,对答案的贡献为j-i,用单调队列维护最值即可 #include<iostream> #include<cstdlib> # ...

  7. 20181015 考试记录&数论

    题目传送门 W神爷的题解 数论 小 M 的算式 [问题描述] 小 M 在做数学作业的时候遇到了一个有趣的问题:有一个长度为 n 的数字 串 S,小 M 需要在数字之间填入若干个“+”和恰好一个“=”, ...

  8. 【BZOJ 3811】玛里苟斯 大力观察+期望概率dp+线性基

    大力观察:I.从输出精准位数的约束来观察,一定会有猫腻,然后仔细想一想,就会发现输出的时候小数点后面不是.5就是没有 II.从最后答案小于2^63可以看出当k大于等于3的时候就可以直接搜索了 期望概率 ...

  9. poco入门

    源码按照poco.然后看README,进行安装. ./configure make make install #include "Poco/ActiveMethod.h" #inc ...

  10. UIViewContentMode的各种效果

    UIViewContentMode的各种效果:   首先它是枚举类型的数据,表示为下所示: typedef enum { UIViewContentModeScaleToFill,           ...